Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a Right "done" Button (UIBarButtonItem) to a UINavigationController

I see that a similar question was asked here: How to add a right button to a UINavigationController? (among others) but its not quite what I'm looking to do and they arent solving my problem.

Essentially, I've created a UIViewController called WebViewViewController with a UIWebView on it which will be shown using presentModalViewController. Essentially its a mini web browser to display a web page while keeping the user in the app rather than launching Safari.

The viewController does the following to get it to show... and the "done" button is meant 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 isnt showing.. any ideas where im going wrong?

like image 252
JMattos Avatar asked Apr 20 '11 02:04

JMattos


3 Answers

Try with 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];

}
like image 156
Jhaliya - Praveen Sharma Avatar answered Nov 09 '22 00:11

Jhaliya - Praveen Sharma


Perhaps you're looking for something more like this:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                                      style:UIBarButtonItemStyleDone target:self 
                                     action:@selector(dismissModalViewControllerAnimated:)];
like image 23
Sam Avatar answered Nov 09 '22 02:11

Sam


self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done"
        style:UIBarButtonItemStylePlain target:self action:@selector(done:)];

Just this one line code displayed done button for me.

like image 37
Pradeep Mittal Avatar answered Nov 09 '22 02:11

Pradeep Mittal