2013-07-10 42 views
0

我想創建一個帶有NavigationBar和TableView的子視圖。在導航欄中應該是一個可以編輯tableView的按鈕。 navigationBar和TableView顯示在子視圖中,但不是barButtonItem。我的代碼中是否有錯誤,或者是否有問題,因爲它是子視圖?UIBarButtonItem未在子視圖中顯示

UIView *tagView = [[UIView alloc] initWithFrame:CGRectMake(0, 325, screenSize.size.width, 200)]; 


         tableView = [[UITableView alloc] initWithFrame:CGRectMake(-10, 40, screenSize.size.width, 150) style:UITableViewStylePlain]; 
         tableView.delegate = self; 
         tableView.dataSource = self; 
         [tagView addSubview:tableView]; 

         UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, tagView.frame.size.width, 45)]; 
         self.navigationItem.leftBarButtonItem = self.editButtonItem; 
         [tagView addSubview:navigationBar]; 

         [self.view addSubview:tagInstruction]; 
         [self.view addSubview:tagView]; 

回答

1

self.navigationItem引用navigationController導航項目,並且您沒有任何導航控制器,您只有自定義的NavigationBar,因此它不工作。
爲了解決這個問題,你必須創建你自己的UIButton並添加到NavigationBar的子視圖中。

0

navigationItem表示在父導航欄中的視圖控制器,而不是在此處構造的導航欄中。將按鈕放入導航欄中。

0

如果我需要一個乾淨的導航條,我總是把我的UIViewControllers放在UINavigationController中。它不會傷害,您可以稍後添加導航功能。然後你可以使用self.navigationItem添加的UIBarButtonItem

0

像斯文表示,self.navigationItem指UINavigationItem針對屬於視圖控制器的父的UINavigationController的UINavigationBar的。如果你想在你剛剛分配的導航欄中設置你的按鈕,試試這個:

UINavigationItem * navigationItem = [[UINavigationItem alloc] init]; 
navigationItem.leftBarButtonItem = self.editButtonItem; 
[navigationBar setItems:@[navigationItem]];