简体   繁体   中英

Set default rightBarButtonItem for UINavigationController

Sup guys,

I have a tabBarController with a navigationController on each tab.

I wanted to set de default right bar button of navigation bar so I wouldn't have to write the same code on 3 different view controllers.

I tried [[UINavigationBar appearance] setRightBarButtonItem:myButton];

but had no success, is says: -[_UIAppearance setRightBarButtonItem:]: unrecognized selector sent to instance

Then I tried to create my own UINavigationController subclass so I could set the button like: self.navigationItem.rightBarButtonItem = myButton but again no success, nothing seems to happen probably because I dont have a navigationItem at this time.

Anyone have another solution?

If you inherit from the UIViewController you can set the right button like this

UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"foobar.png"] style:UIBarButtonItemStylePlain target:self action:@selector(myAction)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];

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