viewDidLoad() 在 segue 之后没有运行



我正在执行从一个表视图到另一个表视图的 segue。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
NSLog("You selected cell number: (indexPath.row)!")
self.performSegue(withIdentifier: "types", sender: productList[indexPath.row])
}

它应该运行由视图控制器的自定义类描述的新 TableView 的viewDidLoad()(我已经在情节提要中声明过(

func viewDidLoad(parent: String) {
print("This should print")
super.viewDidLoad()
//self.typeTableView.delegate = self
//self.typeTableView.dataSource = self
//Set reference
ref = Database.database().reference()
//Retrieve posts
handle = ref?.child(parent).observe(.childAdded, with: { (snapshot) in
let product = snapshot.key as? String
if let actualProduct = product
{
self.productList.append(actualProduct)
self.typeTableView.reloadData()
}
})

}

知道为什么会发生这种情况吗?

将导航控制器嵌入到目标控制器 并使用标识符类型从当前表视图单元格到它进行 segue。 然后在您的之后添加以下方法func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath( {}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "types" {
if let indexPath = tableView.indexPathForSelectedRow {
let object = productList[indexPath.row] as! yourProductType
let controller = (segue.destination as! UINavigationController).topViewController as! YourDestinationViewController
controller.yourProductProperty = object
}
}
}

确保在目标控制器中声明您的产品属性,以便可以访问其中的当前产品对象。

viewDidLoad没有参数,需要一个override子句:

override func viewDidLoad() {
...
}

您的方法签名是

func viewDidLoad(parent: String) {

但它应该是

override func viewDidLoad() {
super.viewDidLoad()
// Your code
}

您是否对表视图单元格使用相同的类? 如果是,则为两个表视图保留不同的标识符[RESUE 标识符]。

相关内容

最新更新