导航控制器上未添加按钮

时间:2012-04-30 20:42:52

标签: ios

- (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;
}

为什么我的按钮没有添加到导航控制器上?

2 个答案:

答案 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

其他一些事情:

  • 没有两个字母的变量名称。 “ac”非常模糊,“addContacts”会提供更多信息,“addContactsViewController”会提供更多
  • 您是否在navigationController子类中实现了自己的UIViewController属性?建议不要这样做,因为它会覆盖navigationController已有的UIViewController属性。给它一个不同的名字。
  • 父视图控制器上的-viewDidLoad方法是指定AddContacts对象的右侧栏按钮的位置吗?相反,请考虑使用代码在AddContacts的实现中设置bar按钮。
相关问题