user1120133
user1120133

Reputation: 3234

display UIViewController

Is this the right way to call UIViewController programmtically when play button is pressed

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];
}

Upvotes: 0

Views: 2262

Answers (1)

Giuseppe Garassino
Giuseppe Garassino

Reputation: 2292

If your UIViewController is stored in a NIB file you can use:

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

Then you can add its view using:

[self.view addSubview:flipViewController.view];

Or show it as a modal view (as suggested by the name of your UIViewController)

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

Take a look at UIViewController Class Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

EDIT: Here is the way to dismiss a modal view using notification.

You have to set an observer in your UIViewController (the one that calls your 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];
    }
}

Now call setObserver in your viewDidLoad.

Remember to remove your observer in dealloc:

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

Now when you want to come back in your modal view call something like this:

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

This last part posts a notification that arrives to your observer. When the observer gets this particular notification calls [self dismissModalViewControllerAnimated:YES]; and your modal view is dismissed.

This is the documentation: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html

Upvotes: 1

Related Questions