简体   繁体   中英

Add UIBarButtonItem to UINavigationController contained in a UITabBarController from AppDelegate

EDIT: Solution : add the buttons to UIViewControllers and no to UINavigationControllers

My application's root view controller is a uitabbarcontroller who contains an array of 5 navigation controllers, all initialized in app delegate. All works good but I have just one problem : I can't add any button in the navigation bar of the controllers contained in the tabbarcontroller from the app delegate. I can set their navigation bar's title, their navigation bar's background etc but when I add a button in their navigation bar, I have no error but the button isn't displayed.

Is anyone know the reason of that ? Is it forbidden to do that ?

Precision 1: I use .xib files for views, I don't use any storyboard.

Precision 2: this is the code I'm using to create the button for each controller :

UIBarButtonItem *myBarBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon-chrono"] style:UIBarButtonItemStylePlain target:nil action:nil];

[ret.navigationItem setRightBarButtonItem:myBarBtn animated:YES];

Precision 3: when I copy this code in each navigation controller viewDidLoad method, it works

EDIT: it's not a problem with the image , the problem is the same when I use initWithTitle method to create the UIBarButtonItem

add .png or .jpg behind image name may be this will help you.

like this.

 UIBarButtonItem *myBarBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon-chrono.png"] style:UIBarButtonItemStylePlain target:nil action:nil];

    [ret.navigationItem setRightBarButtonItem:myBarBtn animated:YES];

OR

Click on your project -> Build Phases -> Copy bundle resource, and see there, your image exist or not. if not then add it using "+" button tapped. see below image. 在此处输入图片说明

Ok, I'd just found the problem ...

My UITabController contains an array of 5 UINavigationController who each contain a UIViewController as root view controller. I tried to add the buttons on UINavigationControllers instead of UIViewControllers ... If I add the button directly in the UIViewControllers it's working !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM