繁体   English   中英

ViewController不启动

[英]ViewController don't launch

我有个问题。
当我选择UICollectionView cell时,我必须转到下一个ViewController ,但是它不起作用。
NavigationController ,推送的ViewController ,我的object已初始化,但我留在当前控制器中
屏幕截图1
怎么了?
为什么NavigationController推送后不了解RoomViewController? 它在ViewControllers列表中不存在:
屏幕截图2
StoryBoard屏幕截图:
屏幕截图3
初始ViewController,它推送MainViewController代码:

func showMainViewController(house: NLHouse) {
        let mainController = self.storyboard?.instantiateViewController(withIdentifier: "MainViewController") as? MainViewController
        if let mainController = mainController {
            mainController.house = house
            self.navigationController?.pushViewController(mainController, animated: true)
            print()
        }
    } 

MainViewController,它推送RoomViewController代码:

class MainViewController: UIViewController {

    @IBOutlet weak var devicesCollectionView: UICollectionView!
    var house: NLHouse?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print()
        devicesCollectionView.reloadData()
    }

}

extension MainViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return house?.rooms?.count ?? 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let roomCell = devicesCollectionView.dequeueReusableCell(withReuseIdentifier: "roomCell", for: indexPath) as? RoomsCollectionViewCell
        if let roomCell = roomCell, let rooms = house?.rooms {
            roomCell.setRoomInfoInCell(room: rooms[indexPath.item])
        }
        return roomCell ?? UICollectionViewCell()
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let roomController = self.storyboard?.instantiateViewController(withIdentifier: "RoomViewController") as? RoomViewController, let rooms = house?.rooms {
            roomController.room = rooms[indexPath.item]
            self.navigationController?.pushViewController(roomController, animated: true)
            print()
        }
    }
} 

推送代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let roomController = self.storyboard?.instantiateViewController(withIdentifier: "RoomViewController") as? RoomViewController, let rooms = house?.rooms {
            roomController.room = rooms[indexPath.item]
            self.navigationController?.pushViewController(roomController, animated: true)
            print()
        }  

推送的类代码:

import UIKit

class RoomViewController: UIViewController {
    @IBOutlet weak var roomNameLabel: UILabel!
    @IBOutlet weak var devicesCollectionView: UICollectionView!
    var room: NLRoom?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        if let room = room {
            if let roomName = room.roomName {
                roomNameLabel.text = roomName
            }
            devicesCollectionView.reloadData()
        }
    }
}

extension RoomViewController: UICollectionViewDelegate, UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return room?.devices?.count ?? 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let deviceCell = devicesCollectionView.dequeueReusableCell(withReuseIdentifier: "deviceCell", for: indexPath) as? DevicesCollectionViewCell
        if let deviceCell = deviceCell, let devices = room?.devices {
            deviceCell.setDeviceInfoInCell(device: devices[indexPath.item])
        }
        return deviceCell ?? UICollectionViewCell()
    }
}

我在您的屏幕截图上看到的是:您拥有的viewController是导航控制器的根目录。 另外两个视图控制器: mainroom
告诉我,您如何展示MainViewController
我假设你把你的MainViewControllerViewController与创造新UINavigationController和设置MainViewController为根的viewController它。 然后,您尝试从第一个UINavigationController推送RootViewController 如果您希望项目正常运行-从情节提要中删除ViewController,请改为连接MainViewController。 这样,当您推动RoomViewController它们将与MainViewController处于同一导航堆栈中。
您需要这样的结构:
在此处输入图片说明

确定将完整项目添加到zip文件中以进行调查。

更新:
在查看ViewController的源代码后,我发现showMainViewController方法是从后台调用的(从API接收数据之后)。 在主队列上调度推送解决了问题。
面向未来:您对UI所做的任何事情都会在主线程中完成。

请加

super.viewWillAppear(animated) 

作为RoomViewController函数下方的第一行

    override func viewWillAppear(_ animated: Bool) 

发生的事情是您正在实例化控制器,但是当您按下viewWillAppear时,它不会调用视图出现所需的已定义方法,因为未调用super.viewWillAppear。

要么

可能会发生以下情况:您调用了以下函数的控制器本身不在导航堆栈中。 确保VC是导航控制器的视图控制器的一部分,那么只有这才有效。

    self.navigationController?.pushViewController(roomController, animated: true)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM