无法将rightBarButtonItem添加到UIViewController

时间:2011-09-05 13:34:05

标签: xcode xcode4 uinavigationcontroller uinavigationitem rightbarbuttonitem

我有下一个代码:

- (id) initWith...{
  if (self == [super initWithNibName:@"EmptyViewController"
     bundle:[NSBundle mainBundle]]){ 
     ...

    back_btn = [[UIBarButtonItem alloc] initWithTitle:@"Back" 
                                                style:UIBarButtonItemStyleBordered 
                                               target:self
                                               action:@selector(backParentViewController)];
    self.navigationItem.leftBarButtonItem = back_btn;
    update_btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                                                               target:self action:@selector(refresh)];
    self.navigationItem.rightBarButtonItem = update_btn;
    self.title = [utils LabelFromKey:@"commons.nav-button.reports"];
        self.navigationItem.title = [utils LabelFromKey:@"commons.nav-button.reports"];
}

但右边的BarButton不可见。

我尝试将代码移动到viewDidLoad,并将按钮设置为navigationController.navigationItem但不显示按钮

self.navigationController.navigationItem.rightBarButtonItem = update_btn;

任何建议¿?

谢谢

2 个答案:

答案 0 :(得分:0)

消灭ivar update_btn。使用

viewDidLoad开始分配
UIBarButtonItem *update_btn = [[UIBarButtonItem alloc] init...;

使用以下声明分配项目:

self.navigationItem.rightBarButtonItem = update_btn;
[update_btn release];

请告诉我这是否适合您。

答案 1 :(得分:0)

很抱歉延迟,为了使它有效,你必须在方法viewDidAppear中应用UIBarButtonItem