отображать UIViewController - PullRequest
       0

отображать UIViewController

0 голосов
/ 30 января 2012

Это правильный способ программного вызова UIViewController при нажатии кнопки воспроизведения

UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc]
                               initWithBarButtonSystemItem:UIBarButtonSystemItemPlay 
                               target:self 
                               action:@selector(playaudio:)];
systemItem1.style = UIBarButtonItemStyleBordered;

-(void) playaudio: (id) sender 
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"theme" 
                                                     ofType:@"mp3"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
    audioPlayer = [[AVAudioPlayer alloc] 
                    initWithContentsOfURL:fileURL error:nil];

    audioPlayer.currentTime = 0;
    [audioPlayer play];
    [fileURL release];  

    UIViewController* flipViewController = [[UIViewController alloc]init];
    [self.view addSubview:flipViewController.view];
}

1 Ответ

1 голос
/ 30 января 2012

Если ваш UIViewController хранится в файле NIB, вы можете использовать:

FlipViewController *flipViewController = [[FlipViewController alloc] initWithNibName:@"flipView" bundle:nil];

Затем вы можете добавить его вид, используя:

[self.view addSubview:flipViewController.view];

Или показать его как модальное представление (как следует из названия вашего UIViewController)

flipViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:flipViewController animated:YES];

Посмотрите на описание класса UIViewController: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

РЕДАКТИРОВАТЬ: Вот способ отклонить модальное представление с помощью уведомления.

Вы должны установить наблюдателя в вашем UIViewController (тот, который вызывает ваш flipViewController):

- (void)setObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(notificationReceived:) 
                                                 name:@"DismissModalView"
                                               object:nil];
}

- (void)notificationReceived:(NSNotification *)notification {
    if ([[notification name] isEqualToString:@"DismissModalView"]) {
        [self dismissModalViewControllerAnimated:YES];
    }
}

Теперь вызовите setObserver в вашем viewDidLoad.

Не забудьте удалить своего наблюдателя из dealloc:

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    // other objects    
    [super dealloc];
}

Теперь, когда вы захотите вернуться в модальный режим, позвоните примерно так:

- (IBAction)dismissMe:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DismissModalView" object:self];
}

Эта последняя часть отправляет уведомление, которое приходит к вашему наблюдателю. Когда наблюдатель получает это конкретное уведомление, звонит [self dismissModalViewControllerAnimated:YES]; и ваш модальный вид отклоняется.

Это документация: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...