开发者社区 问答 正文

请问怎么在UINavigationController中添加向右按钮?

想要在navigation controller 的顶端条添加一个刷新键。不知道为什么不执行。
hearder代码:
`
@interface PropertyViewController : UINavigationController {}
`

添加button代码:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain
                target:self action:@selector(refreshPropertyList:)];        
        self.navigationItem.rightBarButtonItem = anotherButton;
    }
    return self;
}

不知道错误在哪儿

展开
收起
爵霸 2016-05-27 16:14:18 2133 分享 版权
1 条回答
写回答
取消 提交回答
  • 试试在viewDidLoad中实现。一般来说,代码都应该写在这个函数里面,因为一个UIViewController在初始化了之后并不一定会显示出来,在这种情况下viewDidLoad是不会被调用的

    - (void)viewDidLoad {
      [super viewDidLoad];
    
      UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];          
      self.navigationItem.rightBarButtonItem = anotherButton;
      [anotherButton release];
    }
    
     self.navigationItem.rightBarButtonItem= anotherButton; [anotherButton release];}
    2019-07-17 19:18:12
    赞同 展开评论
问答地址: