如何根据uitableview行选择更改导航栏标题?

问题描述 投票:0回答:1

我已经在视图控制器上添加了导航栏。但是,当我运行代码时,此行:let cell = sender as! UITableViewCell有问题。知道如何根据自己的选择添加或更改导航栏标题吗?我的UITableView是一个数组列表。尽管构建成功,但在我之前突出显示的行中,它始终向我显示Thread 1:signal SIGABRT。除此之外,如何根据在UITableView中的选择更改导航栏标题?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.destination is DetailVC {

        let destination = segue.destination as! DetailVC
        let cell = sender as! UITableViewCell
        destination.navigationItem.title = cell.textLabel?.text

        destination.developer = developerArray[(myTableView.indexPathForSelectedRow?.row)!]
        myTableView.deselectRow(at: myTableView.indexPathForSelectedRow!, animated: true)
    }
}
ios swift xcode uinavigationcontroller uinavigationbar
1个回答
1
投票

在目标控制器上创建navigationTitle属性。

 class Destination: UIViewController {
        var navigationTitle  = ""

        override func viewDidLoad() {
            super.viewDidLoad()
            self.navigationItem.title =  title

       }

    }

检查标题控制器时标题是否可用

if segue.destination is DetailVC {

            let destination = segue.destination as! DetailVC

            if let cell = sender as? UITableViewCell , let title = cell.textLabel?.text {
                destination.navigationItem.title = title
            }


            destination.developer = developerArray[(myTableView.indexPathForSelectedRow?.row)!]
            myTableView.deselectRow(at: myTableView.indexPathForSelectedRow!, animated: true)
        }
© www.soinside.com 2019 - 2024. All rights reserved.