ios - 无法启动 UICollectionViewController

标签 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,因为它附加到 Storyboard......有什么解决方案吗?

最佳答案

您需要为 navigationController? AppDelegate 中的第一个 vc 设置导航,而不是像 nil 那样

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

之后做

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

选择 vc ,然后从 Editor

enter image description here


你也可以加载它

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

关于ios - 无法启动 UICollectionViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56888907/

相关文章:

ios - 如何删除Realm中当前数组中不包含的所有模型

ios - 如何计算文件夹的大小?

swift - Xcode 错误 : "/R.swift/rswift: No such file or directory"

ios - 将结果从 Swift 数组传递到 Segue 到描述

用于 https 通信的 IOS 和 Android 可信证书颁发机构

ios - GCD : cancelation of blocks in global concurrent queue

ios - 范围不正确

ios - 如何使用音序器控制振荡器的频率

ios - UIimage 使用 Swift 检测风景图像

swift - Type Any 不符合协议(protocol) Sequence