ios - 在 Swift 中初始化 View Controller

标签 ios swift

所以我的代码有效,但我收到警告:

Null passed to a callee that requires a non-null argument

显然这是因为下面一行:

MyViewController *vc = [[MyViewController alloc] initWithCoder:nil];

MyViewController 是一个 Swift 类。我已经在其中实现了所需的 initWithCoder 方法。

我的问题很简单 - 我应该将什么参数传递给 initWithCoder 来终止警告,或者我使用什么备用 init 策略?

注意:我正在初始化的 View Controller 是用 Swift 编写的。然而,我正在从 Objective-c 类初始化它。

最佳答案

你应该用不同的方式来做。如果您使用 Storyboard:

UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:identifier];

如果storyboard不同

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];

或者来自xib:

MyViewController *controller = [[MyViewController alloc] initWithNibName:nibName bundle:nil];

或者如果它完全来自代码:

MyViewController *controller = [[MyViewController alloc] init];

同时从您的代码中删除 -initWithCoder: 方法

关于ios - 在 Swift 中初始化 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30055109/

相关文章:

ios - 调整嵌入 ScrollView 内的网页 View 的大小

ios - Swift:UIBezierPath bezierPathByReversingPath() 问题

javascript - Canvas 不会在带有 cordova 和 pixi.js 的 iphone 上呈现

android - 如何在 react-native 中添加破折号或虚线边框?

ios - SCNText 不显示

swift - 使用 carthage 的 RealmSwift 框架集成问题

ios - 控制台上未显示 Firebase 崩溃报告 - 初学者

ios - 在 Swift 中附加到字典数组

ios - 如何在不提供登录信息的情况下解决团队供应问题?

IOS是否可以在不强制转换的情况下获取NSFetchedResultsController字段的数据