2012-04-30 45 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的實現中的欄按鈕。