Adding the right done button (UIBarButtonItem) to the UINavigationController

I see that a similar question was asked here: How to add the right button in the UINavigationController? (among others), but this is not exactly what I am looking to do and they do not solve my problem.

Essentially, I created a UIViewController called WebViewViewController, with a UIWebView on it, which will be shown using presentModalViewController. In essence, it is a mini browser for displaying a web page, while the user is in the application instead of launching Safari.

ViewController does the following to show it ... and the "done" button is designed to provide a place to close the browser.

-(IBAction)visitFacebook { WebViewViewController *rootController = [[WebViewViewController alloc] init]; rootController.webURL = @"http://www.facebook.com/"; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootController]; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(done:)]; [navigationController.navigationItem setRightBarButtonItem:doneButton animated:YES]; [navigationController.navigationItem setTitle:@"Facebook"]; if (rootController) { [self presentModalViewController:navigationController animated:YES]; } [doneButton release]; [rootController release]; } 

Unfortunately, the "done" button does not show .. any ideas that I'm wrong in?

+6
source share
3 answers

Try below

 -(IBAction)visitFacebook{ WebViewViewController *rootController = [[WebViewViewController alloc] init]; rootController.webURL = @"http://www.facebook.com/"; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootController]; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(done:)]; rootController.navigationItem.rightBarButtonItem = anotherButton; [navigationController.navigationItem setTitle:@"Facebook"]; if (rootController) { [self presentModalViewController:navigationController animated:YES]; } [doneButton release]; [rootController release]; } 
+10
source

You might be looking for something similar:

 UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissModalViewControllerAnimated:)]; 
+6
source
 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(done:)]; 

Only this line displayed for me is made for me.

+5
source

Source: https://habr.com/ru/post/886285/


All Articles