我在设置 Headers 和向导航栏添加条形按钮时遇到了一些奇怪的困难 .
我的应用程序是Tabbed应用程序 . 问题出在我的第一个标签中 . 第一个选项卡包含一个UINavigationController,它包含一个UITableViewController . 从我的AppDelegate:
UIViewController *firstViewController = [[MyTableViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
_tabBarController = [[TFTabBarController alloc] init];
_tabBarController.delegate = self;
_tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController, nil];
在firstViewController中选择一行时,我在导航堆栈上推送一个UIWebViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *secondViewController = [[MyWebViewController alloc] init];
[self.navigationController pushViewController:secondViewController animated:YES];
}
在拦截WebView中超链接的onclick时,我在导航堆栈上推送另一个UITableView:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
UIViewController *thirdViewController = [[MyTableViewController alloc] init];
[self.navigationController pushViewController:thirdViewController animated:YES];
return NO;
}
return YES;
}
当thirdViewController打开时,其导航栏仅包含后退按钮,其中包含上一个视图的 Headers 以进行导航 . 其 Headers 和右侧栏按钮未显示......
这是我尝试显示它们的方式:
self.title = @"My Title";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Push me" style:UIBarButtonItemStylePlain target:self action:@selector(push)];
我也尝试通过navigationController设置按钮,但仍然没有运气:
self.navigationController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Push me" style:UIBarButtonItemStylePlain target:self action:@selector(push)];
我在这里做错了什么?
1 回答
访问时检查
self.navigationItem
和self.navigationController
是否nil
.从文档:
访问这些成员的好地方是
-viewDidLoad
,而不是在初始化时 .