如何向UINavigationController添加右键? [英] How to add a right button to a UINavigationController?

查看:93
本文介绍了如何向UINavigationController添加右键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个刷新按钮添加到导航控制器的顶部栏但没有成功。

I am trying to add a refresh button to the top bar of a navigation controller with no success.

这是标题:

@interface PropertyViewController : UINavigationController {

}

以下是我尝试添加它的方法:

Here is how I am trying to add it:

- (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;
}


推荐答案

尝试在viewDidLoad中执行此操作。一般来说,你应该推迟任何事情直到那一点,当UIViewController被引入时,它仍然可能在它显示之前很长一段时间,没有必要提前做好工作并且占用内存。

Try doing it in viewDidLoad. Generally you should defer anything you can until that point anyway, when a UIViewController is inited it still might be quite a while before it displays, no point in doing work early and tying up memory.

- (void)viewDidLoad {
  [super viewDidLoad];

  UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];          
  self.navigationItem.rightBarButtonItem = anotherButton;
  // exclude the following in ARC projects...
  [anotherButton release];
}

至于为什么它目前不工作,我不能说在没有看到更多代码的情况下100%确定,但是在init和视图加载之间发生了很多事情,并且您可能正在做一些导致navigationItem在其间重置的事情。

As to why it isn't working currently, I can't say with 100% certainty without seeing more code, but a lot of stuff happens between init and the view loading, and you may be doing something that causes the navigationItem to reset in between.

这篇关于如何向UINavigationController添加右键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆