能否在Storyboard中禁用ViewController?如何消除未用VC的警告?
解决Storyboard中未使用ViewController的警告及“非激活”设置方法
一、无需删除ViewController即可消除警告
你遇到的警告核心原因是:这个名为“Calendar”的ViewController既没有设置入口点(既不是初始ViewController,也没有关联任何segue),也没有配置供运行时调用的Storyboard ID。要消除警告很简单,只需要给它补一个Storyboard ID就行:
- 打开Main.storyboard,选中“Calendar” ViewController
- 打开右侧的Identity Inspector(第三个图标,类似身份证样式)
- 在
Storyboard ID输入框里填一个唯一的标识符,比如CalendarViewController - 保存后警告就会消失,这个VC会完整保留在Storyboard中,完全不影响当前版本的运行,未来需要时随时可以调用。
二、在Storyboard中实现ViewController“非激活”状态
Storyboard本身没有专门的“非激活”开关,但可以通过以下方式达到类似效果,让这个VC在当前版本中完全不参与应用流程:
- 断开所有关联:确保这个VC没有连接任何segue,也不是项目的初始ViewController,这样它就不会被自动加载或触发
- 添加ID但暂不调用:按照上面的方法添加Storyboard ID后,不在当前版本的代码中使用
instantiateViewControllerWithIdentifier:方法加载它,它就会处于“休眠”状态 - 隔离管理:可以把这个VC拖到Storyboard画布的空白区域,和主流程的scenes分开,既方便管理,也能避免误操作关联到现有流程
未来需要启用这个VC时,只需要在代码中添加调用逻辑即可:
// Swift 示例 let calendarVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CalendarViewController") present(calendarVC, animated: true)
// Objective-C 示例 UIViewController *calendarVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CalendarViewController"]; [self presentViewController:calendarVC animated:YES completion:nil];
内容的提问来源于stack exchange,提问作者Gandalf Saxe
相关产品推荐
相关产品推荐

