How to add Storyboard ViewController into SwiftUI Project?

ⅰ亾dé卋堺 提交于 2021-01-24 09:49:05

问题


I am working on my SwiftUI Project and every View is now in SwiftUI, however due to some limitations of SwiftUI I have to add Storyboard's ViewControllers into my SwiftUI project. I am trying this method,

struct AssetsListView: UIViewControllerRepresentable {

var taskID : String
public typealias UIViewControllerType = AssetsListViewController

func makeUIViewController(context: UIViewControllerRepresentableContext<AssetsListView>) -> AssetsListViewController {
    let assetsListVC = AssetsListViewController()
    assetsListVC.taskID = taskID
    return assetsListVC

}
func updateUIViewController(_ uiViewController: AssetsListViewController, context: UIViewControllerRepresentableContext<AssetsListView>) {


}  
}

This works fine and even viewDidLoad() method of my Storyboard's ViewController calls, but I am unable to see any element on my Storyboard Screen. How can I render those elements? Just like the normal Storyboard stuff.


回答1:


You just created controller by class initialiser, to instantiate it from storyboard you have to do like the following

func makeUIViewController(context: 
         UIViewControllerRepresentableContext<AssetsListView>) -> AssetsListViewController {
    let storyboard = UIStoryboard(name: "Main",     // < your storyboard name here
          bundle: nil)
    let assetsListVC = storyboard.instantiateViewController(identifier: 
          "AssetsListViewController")      // < your controller storyboard id here

    assetsListVC.taskID = taskID
    return assetsListVC

}


来源:https://stackoverflow.com/questions/60567727/how-to-add-storyboard-viewcontroller-into-swiftui-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!