Как добавить правую кнопку в UINavigationController? - PullRequest
190 голосов
/ 03 августа 2009

Я пытаюсь добавить кнопку обновления в верхнюю панель контроллера навигации, но безуспешно.

Вот заголовок:

@interface PropertyViewController : UINavigationController {

}

Вот как я пытаюсь это добавить:

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

Ответы [ 22 ]

0 голосов
/ 10 октября 2018

Также вы можете добавить несколько кнопок, используя rightBarButtonItems

-(void)viewDidLoad{

    UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"button 1" style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD1:)];
    UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithTitle:@"button 2" style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD2:)];

    self.navigationItem.rightBarButtonItems = @[button1, button2];
}
0 голосов
/ 20 февраля 2015

Вы должны добавить свой barButtonItem в методе - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated.

...