2012-04-30 48 views
0
- (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; 
} 

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

回答

1

UIBarButtonItem s的未由导航控制器控制,而是通过每个它所包含的视图控制器的 - 每个UIViewController可以有不同的按钮。尝试:

ac = [[AddContacts alloc]init]; 
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];   
ac.navigationItem.rightBarButtonItem = anotherButton; 

然后像您一直在初始化UINavigationController

+0

我照你说我,但是新的问题是,当我点击按钮,我收到SIGABRT问题 – Pamy

+0

请问方法'refreshPropertyList:'存在于这个文件或'AddContacts'类? –

+0

非常感谢............ – Pamy

1

这里有几件事情可能会导致问题。

可能是主要的问题是这行:

self.navigationController.navigationItem.rightBarButtonItem = anotherButton; 

我敢肯定,你想成为什么样的设置是在ac视图控制器的导航项目的右侧栏按钮项目。

ac.navigationItem.rightBarButtonItem = anotherButton 

一些其他的东西,但:

  • 没有这两个字母的变量名。 “ac”非常含糊,“addContacts”将提供更多信息,“addContactsViewController”将提供更多
  • 您是否在UIViewController子类中实现您自己的navigationController属性?不推荐这样做,因为它正在覆盖UIViewController已有的navigationController属性。给它一个不同的名字。
  • 父视图控制器上的-viewDidLoad方法是要分配AddContacts对象的右栏按钮的位置吗?相反,请考虑将代码设置为AddContacts的实现中的栏按钮。