我有问题。
当我选择cell
UICollectionView
时,我必须转到下一个ViewController
,但它不起作用。
NavigationController
推送ViewController
,我的object
已初始化,但我保持当前的控制器
出了什么问题?
为什么推送后的NavigationController不知道RoomViewController?它在ViewControllers列表中不存在:
StoryBoard截图:
初始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()
}
}
答案 0 :(得分:1)
我在屏幕截图中看到的内容:您的viewController是导航控制器的root。另外两个视图控制器:main
和room
告诉我,您如何展示MainViewController
?
我假设您将MainViewController
从ViewController
推送到创建新UINavigationController
并将MainViewController
设置为root viewController。然后,您尝试从RootViewController
开始推送UINavigationController
。
如果您希望项目正常工作 - 从storyboard中删除ViewController,请将MainViewController连接起来。这样,当您按RoomViewController
时,它们将与您的MainViewController
位于同一导航堆栈中
你需要你的结构看起来像这样:
将您的完整项目添加到zip文件中以进行调查。
<强>更新强>
在查看ViewController的suorce代码后,我发现正在从后台调用showMainViewController
方法(从API接收数据后)。派遣主队列上的推送解决了问题
对于未来:你用UI做的任何事情都在主线程中进行。
答案 1 :(得分:-1)
请添加
super.viewWillAppear(animated)
作为RoomViewController函数下面的第一行
override func viewWillAppear(_ animated: Bool)
您正在实例化控制器,但是当您按下viewWillAppear时,不会调用视图显示所需的已定义方法,因为未调用super.viewWillAppear。
或
您调用以下函数的控制器本身可能不在导航堆栈中。确保VC是导航控制器视图控制器的一部分,然后只有这将有效。
self.navigationController?.pushViewController(roomController, animated: true)