0

我需要帮助我UIBarButtonItem没有显示在我的UINavigationBar.

我试图UINavigationBar在我的第三个观点中加入 a 。由于我不太确定是否UINavigationController可行,我决定手动初始化 a UINavigationBar,连同它的UINavigationItem initwithTitle. 该代码有效,但我的问题是添加 aUIBarButtonItem因为它不会显示在UINavigationBar.

- (void)viewDidLoad
{
    [super viewDidLoad];

    UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];

    UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Title"];
    [navigationBar pushNavigationItem:navigationItem animated:NO];

    UIBarButtonItem *button = [[UIBarButtonItem alloc]
                               initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                               target:self
                               action:@selector(showPI:)];
    self.navigationItem.rightBarButtonItem = button;

    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 44.0, self.view.frame.size.width, self.view.frame.size.height - navigationBar.frame.size.height) style:UITableViewStylePlain];
    self.tableView = tableView;

    [self.view addSubview:tableView];
    [self.view addSubview:navigationBar];
    [self showCurrencies];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

我不确定缺少什么。

4

1 回答 1

1

在您的情况下,您正在创建自己的导航栏。尽量不要分配初始化您的导航项,而是使用

UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
UINavigationItem *item = [navigationBar.items lastObject]; 

然后将您创建的 barbuttonItem 设置为项目的 rightBarButtonItem,

item.rightBarButtonItem = button; 

它对我有用。

于 2013-05-30T17:42:06.843 回答