How to add a right button to a UINavigationController?

后端 未结 21 991
不思量自难忘°
不思量自难忘° 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.

    0 讨论(0)
  • 2020-11-28 18:41

    @Artilheiro : If its a navigationbased project, u can create BaseViewController. All other view will inherit this BaseView. In BaseView u can define generic methods to add right button or to change left button text.

    ex:

    @interface BaseController : UIViewController {

    } - (void) setBackButtonCaption:(NSString *)caption;

    (void) setRightButtonCaption:(NSString *)caption selectot:(SEL )selector;

    @end // In BaseView.M

    (void) setBackButtonCaption:(NSString *)caption {

    UIBarButtonItem *backButton =[[UIBarButtonItem alloc] init];
    
    backButton.title= caption;
    self.navigationItem.backBarButtonItem = backButton;
    [backButton release];
    

    } - (void) setRightButtonCaption:(NSString *)caption selectot:(SEL )selector {

      UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
    
    rightButton.title = caption;
    
    rightButton.target= self;
    
    [rightButton setAction:selector];
    
    self.navigationItem.rightBarButtonItem= rightButton;
    
    [rightButton release];
    

    }

    And now in any custom view, implement this base view call the methods:

    @interface LoginView : BaseController {

    In some method call base method as:

    SEL sel= @selector(switchToForgotPIN);

    [super setRightButtonCaption:@"Forgot PIN" selectot:sel];

    0 讨论(0)
  • 2020-11-28 18:43

    It seems that some people (like me) may come here looking for how to add a navigation bar button in the Interface Builder. The answer below shows how to do it.

    Add a Navigation Controller to your Storyboard

    Select your View Controller and then in the Xcode menu choose Editor > Embed In > Navigation Controller.

    Alternatively, you could add a UINavigationBar from the Object Library.

    Add a Bar Button Item

    Drag a UIBarButtonItem from the Object Library to the top navigation bar.

    It should look like this:

    Set the Attributes

    You could double-click "Item" to change the text to something like "Refresh", but there is an actual icon for Refresh that you can use. Just select the Attributes Inspector for the UIBarButtonItem and for System Item choose Refresh.

    That will give you the default Refresh icon.

    Add an IB Action

    Control drag from the UIBarButtonItem to the View Controller to add an @IBAction.

    class ViewController: UIViewController {
    
        @IBAction func refreshBarButtonItemTap(sender: UIBarButtonItem) {
            
            print("How refreshing!")
        }
        
    }
    

    That's it.

    0 讨论(0)
  • 2020-11-28 18:43

    You Can use this:

    Objective-C

    UIBarButtonItem *rightSideOptionButton = [[UIBarButtonItem alloc] initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:@selector(rightSideOptionButtonClicked:)];          
    self.navigationItem.rightBarButtonItem = rightSideOptionButton;
    

    Swift

    let rightSideOptionButton = UIBarButtonItem()
    rightSideOptionButton.title = "Right"
    self.navigationItem.rightBarButtonItem = rightSideOptionButton
    
    0 讨论(0)
  • 2020-11-28 18:43

    Here is the solution in Swift (set options as needed):

    var optionButton = UIBarButtonItem()
    optionButton.title = "Settings"
    //optionButton.action = something (put your action here)
    self.navigationItem.rightBarButtonItem = optionButton
    
    0 讨论(0)
  • 2020-11-28 18:43
        UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add:)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    0 讨论(0)
提交回复
热议问题