我正在尝试更改新导航栏的标题,使其与按下的表格视图单元格匹配,从而将用户移至下一个视图控制器。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
folderCellSelected = folderNames[indexPath.row]
}
获取所选单元格的名称
override func viewDidLoad() {
super.viewDidLoad()
self.title = FoldersViewController.folderCellSelected
// Do any additional setup after loading the view.
mindmapNames.append("Task")
mindmapNames.append("Other Task")
}
应该插入单元格的名称,从而更改新页面的标题
但是我不知道该写些什么而不是FoldersViewController.folderCellSelected
实例成员'folderCellSelected'不能用于类型'FoldersViewController'
是我得到的错误
答案 0 :(得分:0)
您可以使用功能覆盖功能准备(对于segue:UIStoryboardSegue,发件人:任意?) 像这样将值发送到下一个视图控制器: 导入UIKit
class ViewController: UIViewController {
@IBOutlet weak var mytbl: UITableView!
var foldernames = ["test","test1"]
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "next"{
let nextViewController = segue.destination as! NextViewController
nextViewController.myTitle = foldernames[sender as! Int]
}
}
}
extension ViewController : UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return foldernames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = foldernames[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "next", sender: indexPath.row)
}
}
myTitle是NextViewController中的变量
,然后在NextViewController中执行以下操作:
class NextViewController: UIViewController {
var myTitle = ""
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = myTitle
}