How to add a right button to a UINavigationController?

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

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

提交回复
热议问题