无法启动UICollectionViewController

时间:2019-07-04 13:21:20

标签: ios swift

我能够使用以下代码从appdelegate启动FriendsController(UICollectionViewController):

window = UIWindow(frame: UIScreen.main.bounds)window?.makeKeyAndVisible()


    let layout = UICollectionViewFlowLayout()
    let friendsController = FriendsController(collectionViewLayout:  layout)
    window?.rootViewController = UINavigationController(rootViewController: friendsController)

这是FriendsController.swift的外观,您可以自己尝试一下,看看它是否有效:

https://gist.github.com/naderahmedtao/c64d09a3ca62549a015d8df62842b53f

但是,现在我想从ViewController启动FriendsController,但是什么也没发生。

这是我尝试过的:

let friendsController = FriendsController()
        self.navigationController?.pushViewController(friendsController, animated: true)

我也尝试过:

    let layout = UICollectionViewFlowLayout()
        let controller = FriendsController(collectionViewLayout: layout)
   navigationController?.pushViewController(controller, animated: true)

我希望FriendsController可以从VC启动,但事实并非如此,它只能从appdelegate运行。

更新:我刚刚验证了navigationController为nil!但是,我不想从appdelegate启动托管VC,因为它已附加到情节提要...有什么解决方案吗?

1 个答案:

答案 0 :(得分:1)

您需要为AppDelegate中的第一个vc设置导航,以使navigationController?不像nil

let vc = FirstVC()
window?.rootViewController = UINavigationController(rootViewController: vc)

那之后

let friendsController = FriendsController(collectionViewLayout:UICollectionViewFlowLayout())
navigationController?.pushViewController(controller, animated: true)

选择vc,然后从编辑器

enter image description here


您也可以像加载它

 let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FirstID") as! FirstVC
 window?.rootViewController = UINavigationController(rootViewController: vc)
相关问题