gpt4 book ai didi

Swift:我想导航到多个 View Controller

转载 作者:行者123 更新时间:2023-11-30 12:22:40 26 4
gpt4 key购买 nike

当我单击表行时,我想转到多个 View Controller 。所以我编写了如下代码。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{

var targetId = ""
var targetController: UITableViewController!

switch indexPath.row {
case 2:
targetId = "TableView1"
targetController = TableViewController1()
case 2:
targetId = "TableView2"
targetController = TableViewController2()
case 2:
targetId = "TableView3"
targetController = TableViewController3()
default:
return
}

let detailView = self.storyboard?.instantiateViewController(withIdentifier: targetId) as! targetController
self.navigationController?.pushViewController(detailView, animated: true)

}

但我可以看到错误消息“使用未声明的类型'targetController'”。如何导航到多个 View Controller ?

最佳答案

您不能将对象强制转换为变量(实例)。

但是,如果所有 Controller 都是 UIViewController 的子类,则根本不需要类型转换

let detailView = self.storyboard?.instantiateViewController(withIdentifier: targetId)
self.navigationController?.pushViewController(detailView, animated: true)

关于Swift:我想导航到多个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44600592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com