[英]ViewController don't launch
我有個問題。
當我選擇UICollectionView
cell
時,我必須轉到下一個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()
}
}
我在您的屏幕截圖上看到的是:您擁有的viewController是導航控制器的根目錄。 另外兩個視圖控制器: main
和room
。
告訴我,您如何展示MainViewController
?
我假設你把你的MainViewController
從ViewController
與創造新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.