How to add a right button to a UINavigationController?

后端 未结 21 1174
不思量自难忘°
不思量自难忘° 2020-11-28 18:25

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

Here is the header:

@interface PropertyViewController          


        
21条回答
  •  星月不相逢
    2020-11-28 18:41

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

    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.

提交回复
热议问题