Question

I have added a navigation bar programmatically to the MPMoviePlayerViewController. How to add a backbar button item to the navigation bar to navigate to previous view controller?

-(IBAction)playA1{

NSURL * urlA1 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"A1"ofType:@"mp4"]]; 

MPMoviePlayerViewController *playercontrollerA1 = [[MPMoviePlayerViewController alloc] initWithContentURL:urlA1];
 playercontrollerA1.moviePlayer.repeatMode = MPMovieRepeatModeOne;
//    [self presentMoviePlayerViewControllerAnimated:playercontrollerA1]; //comment out if navigation bar code is added
playercontrollerA1.moviePlayer.movieSourceType = MPMovieSourceTypeFile;

// ADD UI NAVIGATION BAR in EMBEDDED CONTROL STYLE

playercontrollerA1.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
UINavigationController *movieNavController = [[UINavigationController alloc] initWithRootViewController:playercontrollerA1];
playercontrollerA1.navigationItem.title=@"APone";
playercontrollerA1.navigationController.navigationBar.tintColor = [UIColor blackColor];

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(favouriteButtonClicked:)];
playercontrollerA1.navigationItem.rightBarButtonItem = button;
[self presentViewController:movieNavController animated:YES completion:nil];

}

Was it helpful?

Solution

I thing you're using UINavigationController as wrong method. When you call presentViewController passing UINavigationController as a ViewController you aren't "navigating". You're just asking for show a viewcontroller.

Is "self" a UIViewController right? If yes, change this to UINavigationController and call pushViewController instead UIViewController's presentViewController method.

OTHER TIPS

Take a look at

But I think the way you have asked the question suggests that you might be getting confused as to the roles of navigationController, navigationBar and associated viewControllers.

You haven't added a navigation bar to the MPMoviePlayerViewController, rather the reverse - you have added the MPMoviePlayerViewController to a Navigation Controller (movieNavController) as it's rootViewController. MovieNavController presents the navigation bar

The standard back bar button item is provided by a navController in it's navigation bar to navigate you back through a stack of contained viewControllers - ending with your rootViewController (the first viewController in the navigation stack). So when you only have a rootViewController on the stack, there is nothing to go back to, and no backBarButtonItem is presented.

If you want to get back from the navigationController to the entity that presented it in the first place, the control for that will generally be found in the presenting object. For example, you might have a set of navigationControllers contained in a UITabBarController - in which case the navigation from one to other of these navigationControllers is controlled via the UITabBarController.

If you do want to add a custom button to your navbar to get you back from that navigationController, the questions linked above should give you a good start.

[self presentViewController:movieNavController animated:YES completion:nil] doesn't seem to make sense in this context, as Ricardo has pointed out.

The following code worked for me.

NSURL * urlA1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]    pathForResource:@"A1"ofType:@"mp4"]];
MPMoviePlayerViewController *playercontrollerA1 = [[MPMoviePlayerViewController alloc] initWithContentURL:urlA1];
playercontrollerA1.moviePlayer.repeatMode = MPMovieRepeatModeOne;
playercontrollerA1.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playercontrollerA1.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
playercontrollerA1.moviePlayer.controlStyle = MPMovieControlStyleNone;
playercontrollerA1.navigationItem.title=@"APone";
playercontrollerA1.navigationController.navigationBar.tintColor = [UIColor blackColor];
[self.navigationController pushViewController:playercontrollerA1 animated:YES];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top