ios - 在 Swift 中从 Storyboard实例化 ViewControllers

标签 ios swift uiviewcontroller uistoryboard

我有一个看起来有点像这样的 UIViewController:

class ProfileViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, ... { 
    ...

    convenience init(name: String) {
        print("Init with student: \(name)")
        self.init()
    }
    ...
}

我有一个相应的 Storyboard 布局,嵌入到 UINavigationViewController 中,链接到 UITabBarController。这似乎是设计布局的最简单方法,并且非常适合只需要此 VC 的一个实例(这就是应用程序最初的设计方式)的情况。

我现在想从这个单一设计(1 到 3 之间)创建多个选项卡,并以编程方式传递 VC 初始化信息,但我不确定执行此操作的最佳方法 - 正如您在上面看到的我已经根据我所做的一些阅读添加了一个便利的 init 函数,因为这似乎是一个很好的起点。

像这样基于 Storyboard 布局创建新的命名选项卡非常容易:

for user in (users)! {
     let storyboard = UIStoryboard(name: "Main", bundle: nil)
     let controller = storyboard.instantiateViewController(withIdentifier: "profileNC") 
     controller.tabBarItem.title = user.name
     newTabs?.insert(controller, at: 0)
}
self.viewControllers = newTabs

但是,这样做我无法调用 init 函数来向 UIViewController 传递正确显示所需的信息。

如何创建我的 ViewController,将布局链接到 Storyboard并使用自定义初始化函数?这可能吗?

非常感谢任何建议!!

最佳答案

使用 Storyboard时,您不能使用自定义初始化程序。您将需要直接设置属性或在创建 View Controller 实例后使用配置函数。

关于ios - 在 Swift 中从 Storyboard实例化 ViewControllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42021915/

相关文章:

ios - watch 套件的语音听写 (Apple Watch)

ios - 如何在我的设备上而不是在 iPhone 6 模拟器中运行我的 React Native 应用程序?

ios - 无法将 CIImage 保存到相机胶卷 - Swift 3

string - 如何使用 Swift 修改字符串中的单个字符?

ios - 使用 Storyboard 在屏幕上显示两个 View Controller ,例如 SplitViewController

ios - Facebook ios SDK - 如何处理提要对话框回调?

iphone - 访问和设置 iPhone 默认闹钟

ios - 为viewController实例创建属性时,找不到viewController

ios - 如何在 iOS Swift 中从本地目录加载index.html 文件?

ios - 使用 .plist 中的数据加载 UIViewController