ios - Swift 将导航栏标题更改为所选表格 View 单元格的标题

标签 ios swift

我正在尝试更改新导航栏的标题以匹配按下的表格 View 单元格,将用户移动到下一个 View Controller 。

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”上使用

这是我得到的错误

最佳答案

您可以使用函数覆盖 funcprepare(for segue: UIStoryboardSegue, sender: Any?) 将值发送到下一个 View Controller ,如下所示: 导入 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
    }

关于ios - Swift 将导航栏标题更改为所选表格 View 单元格的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51122782/

相关文章:

objective-c - 如何删除某些标点符号集并保留字符串中的其他标点符号?

objective-c - 将 UI Picker 添加到 UITextView

iphone - 从 id 查找对象类型

arrays - 如何将动态数据快速获取到数组 KingFisher

iOS/swift : Can't assign optional String to UILabel text property

ios - 从 JSON API 附加到数组

ios - UIToolbar 和 CGRectMake

ios - 一周中给定日期的日期列表 Swift

swift - 我的委托(delegate)函数没有在 Swift 中被调用

swift - 准备segue不传递数据