0

我想创建一个带有 NavigationBar 和 TableView 的子视图。在导航栏中应该是一个可以编辑 tableView 的按钮。navigationBar 和 TableView 显示在子视图中,但 barButtonItem 不显示。我的代码有错误,还是因为它是子视图而有问题?

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];
4

4 回答 4

2

self.navigationItem 指的是 navigationController 导航项,您没有任何导航控制器,您只有自定义 NavigationBar,因此它不起作用。
为了解决这个问题,您必须创建自己的 UIButton 并添加到 NavigationBar 的子视图中。

于 2013-07-10T15:53:12.107 回答
0

navigationItem 表示父导航栏中的视图控制器,而不是您在此处构建的导航栏中。将按钮放入您的导航栏中。

于 2013-07-10T15:49:50.820 回答
0

如果我需要一个干净的导航栏,我总是将我的 UIViewControllers 放在 UINavigationController 中。它没有伤害,您可以稍后添加导航功能。然后你可以使用 self.navigationItem 添加一个 UIBarButtonItem

于 2013-07-10T16:00:09.750 回答
0

正如 Sven 所说,self.navigationItem 指的是 UINavigationBar 的 UINavigationItem,它属于视图控制器的父 UINavigationController。如果您想在刚刚分配的导航栏中设置按钮,请尝试以下操作:

UINavigationItem * navigationItem = [[UINavigationItem alloc] init];
navigationItem.leftBarButtonItem = self.editButtonItem;
[navigationBar setItems:@[navigationItem]];
于 2013-07-10T16:13:23.330 回答