2013-07-10 105 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]];