How to add a right button to a UINavigationController?(如何向 UINavigationController 添加右键?)

本文介绍了如何向 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 添加右键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!