You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在跳转后的含Navigation Controller的Storyboard中添加返回按钮?

嘿,这个问题得看你是用哪种方式跳转到第二个Storyboard的,我给你分两种最常见的情况来讲解,保证你能搞定:

情况1:你用的是push方式跳转(或者通过Storyboard Reference关联到第二个Storyboard的Navigation Controller)

这种情况下,正常来说Navigation Controller会自动自带返回按钮,如果你的界面上没显示,大概率是这两个原因:

  • 你不小心把第二个VC的返回按钮隐藏了:检查代码里有没有navigationItem.hidesBackButton = true,把它改成false就行。
  • 导航栏被隐藏了:在第二个VC的viewDidLoad里加上navigationController?.isNavigationBarHidden = false,确保导航栏显示出来。

要是你想自定义返回按钮的样式(比如改文字),可以在第二个VC的viewDidLoad里手动添加:

// Swift 代码示例
let customBackButton = UIBarButtonItem(title: "回到首页", style: .plain, target: self, action: #selector(goBackToPreviousVC))
navigationItem.leftBarButtonItem = customBackButton

@objc private func goBackToPreviousVC() {
    // 回到上一个VC
    navigationController?.popViewController(animated: true)
}
// Objective-C 代码示例
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:@"回到首页" style:UIBarButtonItemStylePlain target:self action:@selector(goBackToPreviousVC)];
self.navigationItem.leftBarButtonItem = customBackButton;

- (void)goBackToPreviousVC {
    [self.navigationController popViewControllerAnimated:YES];
}
情况2:你用的是present方式模态跳转

这种情况下,第二个Storyboard的Navigation Controller不会自带返回按钮(因为模态出来的是独立的界面栈),这时候需要手动添加返回按钮,并且调用dismiss方法关闭当前界面:

// Swift 代码示例
let backButton = UIBarButtonItem(title: "返回", style: .plain, target: self, action: #selector(dismissCurrentVC))
navigationItem.leftBarButtonItem = backButton

@objc private func dismissCurrentVC() {
    // 关闭当前模态界面,回到第一个VC
    dismiss(animated: true, completion: nil)
}
// Objective-C 代码示例
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(dismissCurrentVC)];
self.navigationItem.leftBarButtonItem = backButton;

- (void)dismissCurrentVC {
    [self dismissViewControllerAnimated:YES completion:nil];
}
额外小提示
  • 如果是通过Storyboard可视化跳转的,记得在Segue的属性面板里,把Segue类型设为Show(Push),而不是Present Modally,这样就能自动获得系统返回按钮啦。

内容的提问来源于stack exchange,提问作者Lucas Bordini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:00:48