Ayubxon Ubaydullayev
Ayubxon Ubaydullayev

Reputation: 349

can't add UIBarButtonItem to navigationController inside of 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]];

Why it's not adding UIBarButtonItem to my navigation controller?

Upvotes: 3

Views: 1673

Answers (3)

Vinodh
Vinodh

Reputation: 5268

You can do something like this

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

Upvotes: 2

Prashanth
Prashanth

Reputation: 463

can you try below code

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

Upvotes: 1

iPatel
iPatel

Reputation: 47099

First remove/comment following line of code

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

Then put this code in viewDidLoad method of viewController of your navContrller.

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

Upvotes: 6

Related Questions