Ios 如何打开带有选定单元格标题的导航控制器

Ios 如何打开带有选定单元格标题的导航控制器,ios,swift,uitableview,Ios,Swift,Uitableview,我希望在选择表格单元格时打开导航控制器。导航控制器必须具有选定单元格中包含的文本的标题 我认为我与我的代码非常接近,但似乎无法找出我做错了什么 从表中选择单元格时,不会打开任何内容 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // Retrieve cell let cellIdentifier: S

我希望在选择表格单元格时打开导航控制器。导航控制器必须具有选定单元格中包含的文本的标题

我认为我与我的代码非常接近,但似乎无法找出我做错了什么

从表中选择单元格时,不会打开任何内容

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        // Retrieve cell
        let cellIdentifier: String = "stockCell"
        let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        myCell.textLabel?.textAlignment = .center
        myCell.textLabel?.font = .boldSystemFont(ofSize: 18)
        // Get the stock to be shown
        let item: StockModel = feedItems[indexPath.row] as! StockModel
        // Configure our cell title made up of name and price


        let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")


        print(titleStr)
        // Get references to labels of cell
        myCell.textLabel!.text = titleStr

        return myCell
    }

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

        let item: StockModel = feedItems[indexPath.row] as! StockModel
        let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")

        print(titleStr)

    }

     func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "customerDetails" {

        let destinationVC = segue.destination as UIViewController
        let cellIdentifier: String = "stockCell"
        let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        destinationVC.navigationItem.title = myCell.textLabel!.text
        }
    }

}
最新更新:

以下各项现在可以发挥作用:

但是我需要视图控制器将其自身呈现为显示细节故事板样式,其中detailController覆盖FirstViewController


无法返回的位置。

检查main.storyboard文件中是否连接了segue。如果是,请检查标识符是否与代码中的标识符匹配。如果这不起作用,可能是因为您正在prepareForSegue中创建一个新的单元格,要解决这个问题,您必须为每个单元格创建一个全局变量。如果所有这些都不起作用,告诉我。可以附加main.storyboard文件。

检查main.storyboard文件中是否连接了segue。如果是,请检查标识符是否与代码中的标识符匹配。如果这不起作用,可能是因为您正在prepareForSegue中创建一个新的单元格,要解决这个问题,您必须为每个单元格创建一个全局变量。如果所有这些都不起作用,告诉我。可以附加main.storyboard文件。

在这种情况下,最好通过编程方式推送视图控制器。一个非常简单的完整示例如下所示:

class ViewController: UIViewController {

    let titles: [String] = ["First", "Second", "Third"]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

}

// MARK: - UITableViewDelegate, UITableViewDataSource

extension ViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        cell.textLabel?.text = titles[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let controller = UIViewController() // or UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "whateverIdentifierISetInStoryboard")
        controller.title = titles[indexPath.row]
        navigationController?.pushViewController(controller, animated: true)
    }

}
因此,使用navigationController?.pushViewControllercontroller,animated:true是一种从右向左显示新视图控制器动画的方法

只需在视图控制器上直接设置标题,即可使用UIStoryboardname:Main、bundle:nil从情节提要创建标题。InstanceEviewControllerWithiIdentifier:WhateVeriIdentifier在情节提要中

这假定此视图控制器实际上已在导航控制器上

如果确实需要使用segue执行此操作,则需要使用标识符手动调用segue。您还可以将标题作为发件人参数发送

self.performSegue(withIdentifier: "customerDetails", sender: titles[indexPath.row])
在您的情况下,可能类似于:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let item: StockModel = feedItems[indexPath.row] as! StockModel
    let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")
    self.performSegue(withIdentifier: "customerDetails", sender: titleStr)
}
现在只需使用以下标题:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "customerDetails" {
        let destinationVC = segue.destination as UIViewController
        destinationVC.title = sender as? String
    }
}
另一种方法是保存属性上的当前标题,然后在准备segue时使用它:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let item: StockModel = feedItems[indexPath.row] as! StockModel
    let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")
    self.nextScreenTitle = titleStr
    self.performSegue(withIdentifier: "customerDetails", sender: self)
}
然后是用法:

var nextScreenTitle: String?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "customerDetails" {
        let destinationVC = segue.destination as UIViewController
        destinationVC.title = nextScreenTitle
    }
}

在这种情况下,最好通过编程方式推动视图控制器。一个非常简单的完整示例如下所示:

class ViewController: UIViewController {

    let titles: [String] = ["First", "Second", "Third"]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

}

// MARK: - UITableViewDelegate, UITableViewDataSource

extension ViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        cell.textLabel?.text = titles[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let controller = UIViewController() // or UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "whateverIdentifierISetInStoryboard")
        controller.title = titles[indexPath.row]
        navigationController?.pushViewController(controller, animated: true)
    }

}
因此,使用navigationController?.pushViewControllercontroller,animated:true是一种从右向左显示新视图控制器动画的方法

只需在视图控制器上直接设置标题,即可使用UIStoryboardname:Main、bundle:nil从情节提要创建标题。InstanceEviewControllerWithiIdentifier:WhateVeriIdentifier在情节提要中

这假定此视图控制器实际上已在导航控制器上

如果确实需要使用segue执行此操作,则需要使用标识符手动调用segue。您还可以将标题作为发件人参数发送

self.performSegue(withIdentifier: "customerDetails", sender: titles[indexPath.row])
在您的情况下,可能类似于:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let item: StockModel = feedItems[indexPath.row] as! StockModel
    let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")
    self.performSegue(withIdentifier: "customerDetails", sender: titleStr)
}
现在只需使用以下标题:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "customerDetails" {
        let destinationVC = segue.destination as UIViewController
        destinationVC.title = sender as? String
    }
}
另一种方法是保存属性上的当前标题,然后在准备segue时使用它:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let item: StockModel = feedItems[indexPath.row] as! StockModel
    let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")
    self.nextScreenTitle = titleStr
    self.performSegue(withIdentifier: "customerDetails", sender: self)
}
然后是用法:

var nextScreenTitle: String?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "customerDetails" {
        let destinationVC = segue.destination as UIViewController
        destinationVC.title = nextScreenTitle
    }
}
您应该在deistinationVC中使用变量,然后在ViewDidLoad中使用navigationItem.title

在firstViewController中:

和第二视图控制器:

您应该在deistinationVC中使用变量,然后在ViewDidLoad中使用navigationItem.title

在firstViewController中:

和第二视图控制器:


在更新版本中执行此操作

let controller = storyboard.instantiateviewcontroller(withIdentifier: "youridentifier") as? "name of your view controller"
navigationController?.present(controller, animated: true, completion: nil)

在更新版本中执行此操作

let controller = storyboard.instantiateviewcontroller(withIdentifier: "youridentifier") as? "name of your view controller"
navigationController?.present(controller, animated: true, completion: nil)

我不确定如何创建一个全局变量,但其他一切都是正确的。我想我在单元格中传递的单元格值文本不正确。我不知道如何创建一个全局变量,但其他一切都是正确的。我想我正在不正确地传递单元格中的单元格值文本。我没有收到任何错误,但是标题没有显示在导航控制器中title@RelsonJames我的道歉。prepareForSegue方法已在最新的Swift中重命名。请检查我的最新答案。您需要为segue:UIStoryboardSegue、sender:Any使用override func preparefor?{它仍然没有显示,可能是单元格中的文本没有正确传递吗?@RelsonJames您的字符串有可能为零或为空。请尝试确认这一点。我建议您在preparefor segue中放置一个断点,然后按代码左侧的一行,应该会显示一个蓝色的项目符号。当您运行项目时,它应该会停止一个断点在那一行,你可以检查所有的值。此外,你可以在那一点上使用你的控制台右下部分的Xcode来打印你有权访问的值。你通常使用po来打印你的对象。在你的情况下,尝试po sender as?String。这应该会给你你创建的字符串。如果它是nil,那么在PerformScheue检查它。发件人:titleStr是cor
performsgue中直接定义,但destinationVC.tlte=sender as中为nil?StringI没有收到任何错误,但标题不会显示在导航控制器中title@RelsonJames我的道歉。prepareForSegue方法已在最新的Swift中重命名。请检查我的最新答案。您需要为segue:UIStoryboardSegue、sender:Any使用override func preparefor?{它仍然没有显示,可能是单元格中的文本没有正确传递吗?@RelsonJames您的字符串有可能为零或为空。请尝试确认这一点。我建议您在preparefor segue中放置一个断点,然后按代码左侧的一行,应该会显示一个蓝色的项目符号。当您运行项目时,它应该会停止一个断点在那一行,你可以检查所有的值。此外,你可以在那一点上使用你的控制台右下部分的Xcode来打印你有权访问的值。你通常使用po来打印你的对象。在你的情况下,尝试po sender as?String。这应该会给你你创建的字符串。如果它是nil,那么在PerformScheue检查它。发件人:titleStr在performsgue中定义正确,但在destinationVC.tlte=sender中定义为?string别忘了在distinitionvc中定义var navTitle=string使用push导航>>让vc=storyboard?。实例化eviewcontrollerwhidentifier:customerDetails as!customerDetailsVC.和>>vc.navTitle=titleStr和>>导航控制器?.pushViewControllervc,动画:True别忘了在DistinationvC中定义var navTitle=string使用push导航>>让vc=storyboard?。实例化EviewController WithiIdentifier:customerDetails as!customerDetailsVC.和>>vc.navTitle=titleStr和>>navigationController?。pushViewControllervc,动画:True我认为可能的复制品我认为这是可能的复制品。