【问题标题】:Can't set the title of the back bar button item of a view controller无法设置视图控制器的后栏按钮项的标题
【发布时间】:2012-11-29 21:27:35
【问题描述】:

我正在使用具有导航控制器的应用程序训练自己,并且用户可以在两个视图控制器之间切换。
根视图控制器类称为 ViewController,这就是我在应用程序委托中以编程方式添加导航控制器的方式:

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
navController= [[UINavigationController alloc]initWithRootViewController: viewController];
self.window.rootViewController= navController;

这是导航控制器:

@property (strong, nonatomic) UINavigationController* navController;

第二个视图控制器被称为 SecondViewController。每当用户点击条形按钮项时,我都会推送它:

self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle: @"Second View" style: UIBarButtonItemStylePlain target: self action: @selector(switchView:)];

这是执行的选择器:

- (void) switchView : (id) sender
{
    if(!nextController)
    {
        nextController= [[SecondViewController alloc]initWithNibName: @"SecondViewController" bundle: nil];
    }
    [self.navigationController pushViewController: nextController animated: YES];
}

然后在第二个视图控制器中自动出现一个标题为“返回”的按钮,它切换到第一个视图。这对我来说很好,但我想设置标题和样式。所以我正在尝试更改标题:

self.navigationItem.backBarButtonItem.title= @"First View";

但是无事可做,标题还是“回”,怎么改?

另外,我是不是采用了错误或低效的方法,对吧?

【问题讨论】:

    标签: objective-c uinavigationcontroller uibarbuttonitem xcode4.5 osx-mountain-lion


    【解决方案1】:

    如果您在viewDidLoad 中设置ViewController 类的title 属性,则当您将SecondViewController 压入堆栈时,该字符串将显示为后退按钮的文本。

    【讨论】:

      【解决方案2】:

      试试这个来改变按钮的标题。初始化并添加您自己的 leftBarButtonItem 并隐藏股票返回按钮。

      UIBarButtonItem* backBtn =  [[UIBarButtonItem alloc] initWithTitle:@"BUTTON_NAME" style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];
      
      self.navigationItem.leftBarButtonItem = backBtn;
      self.navigationItem.hidesBackButton = YES;
      

      如果可以的话,您将想要隐藏原始的后退按钮,并且只需制作一个 backAction,它可以是使用 popViewcontrollerAnimated 函数的东西(如果您一开始就按下 viewcontroller)。

      - (void) backAction {
          [self.navigationController popViewControllerAnimated:YES];
      }
      

      【讨论】:

      • 你成就了我的一天
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多