添加一个正确的“完成”button(UIBarButtonItem)到UINavigationController

我看到在这里问了一个类似的问题: 如何添加一个右键到UINavigationController? (等),但它不是我想要做的,他们不是解决我的问题。

从本质上讲,我创build了一个名为WebViewViewController的UIViewController,其上有一个UIWebView,它将使用presentModalViewController进行显示。 本质上,它是一个迷你的网页浏览器,用于显示网页,同时保持用户在应用程序,而不是启动Safari。

viewController执行以下操作来显示…“完成”button是为了提供一个closures浏览器的地方。

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

不幸的是,“完成”button没有显示..任何想法,我会出错?

试试下面

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

也许你正在寻找更像这样的东西:

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

只是这一行代码显示完成button为我。