无法将UIBarButtonItem添加到UITabBarController内的navigationController

时间:2014-03-14 12:40:17

标签: ios iphone objective-c uinavigationcontroller uitabbarcontroller

ViewController *VC = [[ViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *NavCon = [[UINavigationController alloc] initWithRootViewController:VC];
NavCon.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleBordered target:self action:@selector(showall)];
[self.myTabBarController setViewControllers:@[NavCon]];

为什么它没有将UIBarButtonItem添加到我的导航控制器?

3 个答案:

答案 0 :(得分:6)

首先删除/评论以下代码行

NavCon.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleBordered target:self action:@selector(showall)];

然后将此代码放在navContrller的viewController的viewDidLoad方法中。

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleBordered target:self action:@selector(showall)];

答案 1 :(得分:2)

你可以做这样的事情

UITabBarController *my = [[UITabBarController alloc]init];
    UIViewController *new =  [[UIViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:new];
    new.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"+" style:UIBarButtonItemStyleBordered target:new action:@selector(addButtonClicked:)];
    [my setViewControllers:@[nav]];

答案 2 :(得分:1)

你可以尝试下面的代码

VC.navigationController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleBordered target:self action:@selector(showall)];
[self.myTabBarController setViewControllers:@[NavCon]];