6

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?

Community
  • 1
  • 1
JMattos
  • 202
  • 1
  • 2
  • 13

3 Answers3

10

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

}
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • Many Many thanks. the key was, as I thought, my lack of understanding. – JMattos Apr 20 '11 at 03:48
  • 4
    The key was that the navigationItem in question is an attribute of the root controller, not the navigationController! Correct: `rootController.navigationItem.rightBarButtonItem = doneButton;` inCorrect: `[navigationController.navigationItem setRightBarButtonItem:doneButton animated:YES];` – JMattos Apr 20 '11 at 03:50
  • @JMattos consider marking this as an answer if it helped solved your problem. – Hemanshu Bhojak Feb 02 '12 at 06:43
6

Perhaps you're looking for something more like this:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                                      style:UIBarButtonItemStyleDone target:self 
                                     action:@selector(dismissModalViewControllerAnimated:)];
Sam
  • 1,504
  • 2
  • 13
  • 19
  • Hmm.. Same thing. I think there's some larger thing I'm missing. I'm wading through http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/NavigationControllers/NavigationControllers.html but i'm not seeing my solution. – JMattos Apr 20 '11 at 03:10
5
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done"
        style:UIBarButtonItemStylePlain target:self action:@selector(done:)];

Just this one line code displayed done button for me.

David Lari
  • 943
  • 11
  • 21
Pradeep Mittal
  • 595
  • 1
  • 8
  • 19