0

我有一个UIViewController,我在InterfaceBuilder中添加了一个navigationItem来查看标题,我发现我无法将backBarButtonItem拖到navigationItem,只能添加leftBarButtonItem和rightBarButtonItem。所以我尝试在我的控制器代码中将 backBarButtonItem 添加到 navigationItem 元素:

- (void)viewWillAppear:(BOOL)animated {
   UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"   style:UIBarButtonItemStyleDone target:nil action:nil];
   self.myNavigationItem.backBarButtonItem = _backButton;
  [_backButton release];
}

但是视图上仍然没有 backBarButtonItem?如何解决?

4

1 回答 1

1

您必须实际设置 leftBarButtonItem,而不是 backBarButtonItem,然后才能像这样设置 self.hidesBackBarButtonItem:

- (void)viewWillAppear:(BOOL)animated {
 UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"   style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.hidesBackButtonItem = YES;
     self.myNavigationItem.leftBarButtonItem = _backButton;
    [_backButton release];
}
于 2012-04-21T06:02:16.027 回答