【发布时间】:2020-09-07 12:06:28
【问题描述】:
我正在为一个学校项目创建一个应用程序,并且我有一个带有标签的 tableView,其中包含单元格的名称,我试图将标签的名称传递给我的第二个 viewController 上的 navTitle 变量.我尝试了多种方法来实现这一点,但都没有运气。
我尝试打印传递的标题,但它打印输出'nil'
主页视图
var names = ["Accomodation", "Facilities", "Things To Do", "About"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "homeCell", for: indexPath) as? homeTableViewCell
cell?.label.text = names[indexPath.row]
return cell!
}
//Passing data
var nameChosen : String?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath, animated: true)
nameChosen = names[indexPath.row]
performSegue(withIdentifier: "accomodationSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as? accomodationViewController
destinationVC?.navTitle = nameChosen
}
住宿视图
var navTitle: String?
override func viewDidLoad() {
super.viewDidLoad()
print(navTitle)
// Do any additional setup after loading the view.
}
【问题讨论】:
-
是否调用了
prepareForSegue?
标签: ios swift uitableview