- (void)viewDidLoad
{
[super viewDidLoad];
ac = [[AddContacts alloc]init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:ac];
[self.view addSubview:navigationController.view];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];
self.navigationController.navigationItem.rightBarButtonItem = anotherButton;
}
为什么我的按钮没有添加到导航控制器上?
答案 0 :(得分:1)
UIBarButtonItem
不是由导航控制器控制的,而是由它包含的每个视图控制器控制 - 每个UIViewController
可以有不同的按钮。尝试:
ac = [[AddContacts alloc]init];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];
ac.navigationItem.rightBarButtonItem = anotherButton;
然后像你一直那样初始化UINavigationController
。
答案 1 :(得分:1)
这里有一些可能导致问题的事情。
这一行的主要问题可能是什么:
self.navigationController.navigationItem.rightBarButtonItem = anotherButton;
我非常确定您要设置的内容是ac
视图控制器导航项上的右侧栏按钮项。
ac.navigationItem.rightBarButtonItem = anotherButton
其他一些事情:
navigationController
子类中实现了自己的UIViewController
属性?建议不要这样做,因为它会覆盖navigationController
已有的UIViewController
属性。给它一个不同的名字。-viewDidLoad
方法是指定AddContacts对象的右侧栏按钮的位置吗?相反,请考虑使用代码在AddContacts的实现中设置bar按钮。