设定UINavigationController返回按钮

    //设定标题
    self.navigationItem.title = @"title";
    //设定返回按钮
    self.navigationItem.backBarButtonItem =
    [[[UIBarButtonItem alloc] initWithTitle:@"返回"
                                      style:UIBarButtonItemStylePlain
                                     target:nil
                                     action:nil] autorelease];

原文: http://www.dimzzy.com/blog/2010/11/custom-back-button-title-when-using-uinavigationcontroller/


之前大家是否疑惑为什么设置了类似这样的代码
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]

                                                                  initWithTitle:"返回" 

                                                                  style:UIBarButtonItemStylePlain 

                                                                  target:self 

                                                                  action:nil];

self.navigationItem.backBarButtonItem = backButton;

复制代码界面上backButton并没出现“返回”的字样

其实是被leftBarButtonItem和rightBarButtonItem的设置方法所迷惑了
lleftBarButtonItem和rightBarButtonItem设置的是本级页面上的BarButtonItem,而backBarButtonItem设置的是下一级页面上的BarButtonItem
比如:两个ViewController,主A和子B,我们想在A上显示“刷新”的右BarButton,B上的BackButton显示为“撤退”
就应该在A的viewDidLoad类似方法中写:
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]

                                                                  initWithTitle:"刷新" 

                                                                  style:UIBarButtonItemStylePlain 

                                                                  target:self 

                                                                  action:nil];
self.navigationItem.rightBarButtonItem = refreshButton;

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]

                                                                  initWithTitle:"撤退" 

                                                                  style:UIBarButtonItemStylePlain 

                                                                  target:self 

                                                                  action:nil];

self.navigationItem.backBarButtonItem = cancelButton;

复制代码而B不需要做任何处理
然后ApushB就可以了

你可能感兴趣的:(uibarbuttonitem)