在 AppDelegate 上实例化一个视图控制器,理解

Instantiate a view controller on AppDelegate, understanding

我正在学习 iOS 按照一些教程进行开发。其中一个说: Create a new view controller, call it StoreViewController, and inside the app delegate instantiate a new object for this controller and put it inside a new navigation controller

我创建了一个新的 ViewController,包含 .h .m 和 .xib 文件,并将这段代码放在 AppDelegate.m didFinishLaunchingWithOptions 方法中:

StoreViewController *trStore = [[StoreViewController alloc] init];

现在我在 ...and put it inside a new navigation controller 看到了,但我无法理解。我有一个带有 NavigationController 的故事板,我是否必须 link 这个 StoreViewController 我的导航控制器?你懂什么??

提前致谢

您混淆了实例化视图控制器的不同方法。

当您使用情节提要时,您的主视图控制器会从该情节提要中实例化。只需在故事板中添加一个新的 UIViewController 场景,然后在 Inspector 中将 class 设置为 StoreViewController,如下所示:

然后,将其添加为 UINavigationController 的 rootViewController,如下所示:

编辑: 请尽量只使用适合您目标 xcode/ios 版本的教程。过去几年发生了很多变化,不建议在不了解这些变化的情况下使用旧教程,因为它会导致这样的问题。