Cocoa touch 如何在UINavigationController中为所有ViewController添加UIBarButtonim
我想知道是否可以将按钮直接添加到Cocoa touch 如何在UINavigationController中为所有ViewController添加UIBarButtonim,cocoa-touch,ios,uinavigationcontroller,Cocoa Touch,Ios,Uinavigationcontroller,我想知道是否可以将按钮直接添加到导航控制器,或者我必须将所有视图控制器中的按钮添加到导航控制器 示例:我有3个UIViewController(VC1、VC2和VC3),我可以将这些元素推送到NavigationController(NC)中。如果我需要添加一个按钮作为VC1的右导航项,我可以在VC1的viewdiload中写入: UIBarButtonItem *settingsBtn = [[UIBarButtonItem alloc]initWithTitle:@"Settings" st
导航控制器
,或者我必须将所有视图控制器
中的按钮添加到导航控制器
示例:我有3个UIViewController
(VC1、VC2和VC3),我可以将这些元素推送到NavigationController(NC)中。如果我需要添加一个按钮作为VC1的右导航项,我可以在VC1的viewdiload
中写入:
UIBarButtonItem *settingsBtn = [[UIBarButtonItem alloc]initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered target:self action:@selector(settings)];
self.navigationItem.rightBarButtonItem = settingsBtn;
如果我还需要在NC上为VC2和VC3添加此按钮,我必须在VC2和VC3中添加此代码,并在VC1、VC2和VC3中指定方法“设置”
是否可以以共享方式添加此按钮?我如何将设置方法定义为共享方法?一种方法是使用您的
UIViewController
派生的父类。在父类中,可以在中设置按钮,例如,viewDidLoad
。在你的孩子们的课堂上,你会做[super-viewDidLoad]代码>在viewDidLoad
方法中
希望这有帮助 成为UINavigationController的代表
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated {
UIBarButtonItem *settingsBtn = [[UIBarButtonItem alloc]initWithTitle:@"Settings"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(settings)];
viewController.navigationItem.rightBarButtonItem = settingsBtn;
}
是的,这是一个快速且面向对象的解决方案:)我将为您介绍900点信誉点:P再次感谢您donkim!很乐意帮忙!我从这个社区学到了很多东西,很高兴能为社区做出贡献。:)