• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - Storyboard - initWithCoder 被调用两次

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:44:37 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想知道的重要一点是我正在使用 Storyboard。我在 didFinishLaunchingWithOptions 中执行以下操作:

UIStoryboard *sb = [UIStoryboard storyboardWithName"Storyboard" bundle:nil];

FirstView *mvc = [sb instantiateViewControllerWithIdentifier"FirstView"];
SecondView *tvc = [sb instantiateViewControllerWithIdentifier"SecondView"];

NSArray* controllers = [NSArray arrayWithObjects:[self addNavigationToViewController:mvc], [self addNavigationToViewController:tvc], nil];

// add controllers to a table view
UITabBarController *tb = [[UITabBarController alloc] init];
[tb setViewControllers:controllers];

[self.window setRootViewController:tb];

addNavigationToViewController 是我用来在 UINavigationController 中嵌入 View Controller 的方法

当我运行这个应用程序时,FirstView 中的 initWithCoder 会被调用两次。我找到了原因,但我不知道如何正确处理。

基本上,FirstView 被初始化一次,因为它是 Initial Scene(属性检查器中有一个复选标记设置 View “是初始 View Controller ”),第二次: p>

FirstView *mvc = [sb instantiateViewControllerWithIdentifier"FirstView"];

如果我取消选中 Initial Scene,它会提示“可能未设置指定的入口点”。

我做错了什么,但我不知道它是什么。

任何提示将不胜感激。



Best Answer-推荐答案


在使用 Storyboard的应用程序中,在应用程序启动时,UIKit 在信息列表中查找 the UIMainStoryboardFile key .如果设置了 UIKit 引用该 Storyboard并使用 -[UIStoryboard instantiateInitialViewController] 创建一个默认的 Root View Controller 以附加到窗口。

UIKit 正在这样做,然后在 -didFinishLaunchingWithOptions: 中,您还创建了 View Controller 的新实例。这就是它被实例化两次的原因。

如果您想完全自定义此过程,并且看起来确实如此,那么您可能不希望 UIKit 代表您执行这些默认操作。您可以简单地从 info.plist 中删除 UIMainStoryboardFile 键,并在覆盖 -didFinishLaunchingWithOptions: 时:

  1. 创建一个窗口
  2. 获取 Storyboard
  3. 从 Storyboard 中实例化 View Controller 。
  4. 设置窗口的 Root View Controller 。
  5. 使窗口键可见。

关于iphone - Storyboard - initWithCoder 被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11285333/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap