在Storyboard中配置View Controller的导航项的backBarButtonItem

时间:2012-03-22 18:59:35

标签: interface-builder uibarbuttonitem uinavigationitem uistoryboard

将条形按钮项拖放到Interface Builder中的故事板中的视图控制器导航栏上非常容易。通过这种方式,您可以设置视图控制器导航项的leftBarButtonItemrightBarButtonItem个出口。但是还有一个backBarButtonItem出口,并且根本不明确如何设置它。如何使用Interface Builder设置自定义后退栏按钮项?

4 个答案:

答案 0 :(得分:18)

  • 选择要更改其导航项的视图控制器。显示视图控制器标识的黑条将更改为其引用对象的图标化托盘。

  • 将对象库中的条形按钮项拖放到托盘上。

Storyboard view of a navigation controller and subordinate table view controller

  • 右键单击左侧主对象托盘中的视图控制器导航项。将新添加的按钮连接为导航项目的backBarButtonItem插座。

Interface Builder screenshot illustrating how to wire up the back bar button outlet

  • 选择条形按钮,并使用“属性”检查器以任何方式对其进行配置。

Screenshot of the attributes inspector in Interface Builder showing options for the back bar button item

答案 1 :(得分:16)

正如上面提到的@wcochran,当使用viewControllers推送到navigationController的堆栈时,backBarButtonItem出口已经连线并且无法更改。此外,选择子VC的navigationItem并更改IB中的后退按钮文本并不能达到预期效果。

现在您可能认为替换子VC的backBarButtonItem可以解决问题,但事实并非如此。令人困惑的是,如果你想设置子VC的后退按钮的标题,你必须设置其(!)的后退按钮标题,如下所示:

- (void)viewWillAppear:(BOOL)animated // in the parent VC!
{
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backButton;
}

这对父VC没有任何作用。实际上,如果父对象是navigationController的RootViewController,则根本就没有后退按钮。但是孩子将继承(或拿起)你创建的后退按钮。

这仅适用于直接子VC,因此如果您想通过navigationController的堆栈维护标签,则需要在每个父级上设置它。

感谢#iphonedev中的@wiliz向我解释这一点。

答案 2 :(得分:3)

正如上面提到的@AdamBlock,你必须在父VC中设置正确的东西。

他展示了如何以编程方式执行此操作。也可以在界面构建器中执行此操作。

  • 选择父VC
  • 选择导航项
  • 打开“属性”检查器
  • 设置后退按钮的标题。

答案 3 :(得分:1)

在Interface Builder中,您可以更改导航项目后退按钮的标题。

以编程方式,您可以在视图控制器的viewDidLoad方法中设置自定义后退按钮。在此示例中,我们将按钮的图像设置为名为“customImage.png”的图像:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Custom" style:UIBarButtonItemStyleBordered target:self action:nil];

    // Set custom image here
    backButton.image = [UIImage imageNamed:@"customImage.png"];
    self.navigationItem.backBarButtonItem = backButton;
}