【问题标题】:UINavigationController No Back ButtonUINavigationController 没有返回按钮
【发布时间】:2023-03-26 14:03:01
【问题描述】:

非常简单的场景,有很多关于此的帖子,但我仍然卡住了。我有一个嵌入在UINavigationController 中的UITableView 有一个最终的UIViewController,应该在选择UITableViewCell 时显示。当我将所有这些连接起来时,行选择没有任何反应。

我可以获得由 usind didSelectRowAtIndexPath 呈现的详细视图,并通过其 ID 引用 segue。但是,当我这样做时没有返回按钮。

类 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
        }
    }
}

其他几点说明: 1. 我的单元格标识符在 IB 中设置为“单元格”
2. 我的 segue 在 IB 中设置为“show”,标识符为“contentSegue”
3. segue 是从原型单元到内容视图控制器。

我完全不知所措。任何人都可以帮忙吗?谢谢。

【问题讨论】:

  • 请提供您的下一个视图控制器代码
  • 目前还没有。这只是一个空的 viewDidLoad() 方法。
  • 尝试删除你的segue并重新创建它,有时它会有所帮助,也许你是从UITabeView或Cell创建的,而不是ViewController
  • 谢谢。我已经试过几次了。
  • 你在故事板中做了什么样的转场?

标签: ios uitableview swift3 uinavigationcontroller


【解决方案1】:

您确定将其设置为选择转场,而不是辅助动作吗?您可以通过 ctrl 单击故事板中的单元格来检查这一点 - 确保您触发的 segue 是选择。

还有……

由于您已在情节提要中添加了 segue,因此无需在 didSelectRowAt 中触发它 - 因此您可以删除该函数。

而且,要消除对字符串比较的依赖(并消除强制解包选项),试试这个……

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
    }
}

【讨论】:

  • 感谢您的回复。是的,这是一个选择segue。我已经编辑了我的帖子。另外,我知道我不需要 didSelectRowAt,但我添加了它,因为我的点击事件没有注册。我希望能够完全删除它。
猜你喜欢
  • 2011-03-20
  • 2014-03-20
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-30
相关资源
最近更新 更多