简体   繁体   English

UINavigationController没有后退按钮

[英]UINavigationController No Back Button

Very simple scenario with lots of posts on this, but I'm still stuck. 非常简单的场景,上面有很多帖子,但是我仍然很困惑。 I have a UITableView that is embedded in a UINavigationController There is a final UIViewController that is supposed to be presented when the UITableViewCell is selected. 我有一个UITableView嵌入在一个UINavigationController有一个最终UIViewController是应该的当将要展现UITableViewCell选择。 When I wire all of this up, nothing happens on the row selection. 当我将所有这些连接起来时,行选择没有任何反应。

I can get the detail view to be presented by usind didSelectRowAtIndexPath and referencing the segue by its id. 我可以获得由usind didSelectRowAtIndexPath呈现的详细视图,并通过其ID引用segue。 However, there is no back button when I do this. 但是,执行此操作时没有后退按钮。

在此处输入图片说明

在此处输入图片说明 class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 类MainViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var tableview: UITableView!
    var configJson: JSON = []
    var config: [Tab] = []

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

    func parseConfig() {
        let configParser = ConfigParser(configJson: configJson)
        config = configParser.parse()
    }

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

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

        let tab = self.config[indexPath.row]

        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")

        cell.textLabel?.text = tab.title

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "contentSegue", sender: self)
    }

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

        if (segue.identifier == "contentSegue") {
            let nextVC = segue.destination as? ContentViewController
            let indexPath = self.tableview.indexPathForSelectedRow
            let tab = self.config[(indexPath?.row)!]
            nextVC?.tab = tab
        }
    }
}

A couple of other notes: 1. My cell identifer is set to 'cell' in the IB 其他一些注意事项:1.我的单元格标识符在IB中设置为“单元格”
2. My segue is set to 'show' with a identifier of 'contentSegue' in the IB 2.我的segue在IB中设置为“显示”,标识符为“ contentSegue”
3. The segue is from the prototype cell to the Content View Controller. 3.任务是从原型单元到Content View Controller。

I'm at a total loss. 我完全不知所措。 Can anyone help? 有人可以帮忙吗? Thanks. 谢谢。

Are you sure you've set this up as a selection segue, and not an accessory action? 您确定已将其设置为选择功能,而不是辅助操作吗? You can check this by ctrl clicking on the cell in the storyboard - make sure your triggered segue is selection. 您可以通过ctrl单击情节提要中的单元格进行检查-确保已选择触发触发的segue。

Also… 也…

As you've added the segue in your storyboard, there's no need to fire it in didSelectRowAt - so you can remove that func. 当您在情节提要中添加了segue时,无需在didSelectRowAt触发它-因此您可以删除该功能。

And, to remove your reliance on a string comparison (and remove a force unwrapped optional), try this… 并且,要消除对字符串比较的依赖(并消除强制展开的可选字符),请尝试此操作…

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let nextVC = segue.destination as? ContentViewController, 
       let cell = sender? as UITableViewCell,
       let indexPath = tableview.indexPath(for: cell) {

        let tab = self.config[indexPath.row]
        nextVC.tab = tab
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM