将正确的“完成”按钮(UIBarButtonItem)添加到UINavigationController

时间:2011-04-20 02:59:23

标签: iphone ios uinavigationcontroller uibarbuttonitem

我看到在这里问了一个类似的问题:How to add a right button to a UINavigationController?(除其他外)但它不是我想做的事情,他们也没有解决我的问题。

基本上,我已经创建了一个名为WebViewViewController的UIViewController,其上有一个UIWebView,它将使用presentModalViewController显示。基本上它是一个迷你Web浏览器,用于显示网页,同时将用户保留在应用程序中,而不是启动Safari。

viewController执行以下操作以使其显示...并且“完成”按钮旨在提供关闭浏览器的位置。

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

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

3 个答案:

答案 0 :(得分:10)

尝试使用以下

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

}

答案 1 :(得分:6)

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

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

答案 2 :(得分:5)

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

这一行代码显示为我完成了按钮。