0
votes

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

2
Are you sure your project contains image named "icon-chrono"? I have tried same code in one of my project and it runs fine. My only guess is that image might not be added in the project. - Pratik Mistry
Hi, thank's for your response. Yes i'm sure this image exists, I'd check twice and this image is correctly loaded when I execute this code from a controller's viewDidLoad method. - AdrienTorris
I find another way to do that (I created a UINavigationController extension who create navbar buttons and call it in my controllers viewDidLoad method) so I'm not blocked but I really would like understand what happens here. I thank it was a restriction of Xcode but If it works for you it's strange. Don't understand where I'm wrong ... - AdrienTorris

2 Answers

0
votes

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. enter image description here

0
votes

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 !