菜鸟教程小白 发表于 2022-12-11 18:37:22

ios - UINavigationController 没有返回按钮


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

<p>我可以通过使用 <code>didSelectRowAtIndexPath</code> 获取详细 View ,并通过其 id 引用 segue。但是,当我这样做时没有返回按钮。</p>

<p> <a href="/image/BZ2uB.png" rel="noreferrer noopener nofollow"><img src="/image/BZ2uB.png" alt="enter image description here"/></a> </p>

<p> <a href="/image/Z5x7y.png" rel="noreferrer noopener nofollow"><img src="/image/Z5x7y.png" alt="enter image description here"/></a>
    类 MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {</p>

<pre><code>    @IBOutlet weak var tableview: UITableView!
    var configJson: JSON = []
    var config: = []

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

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

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

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {

      let tab = self.config

      let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: &#34;cell&#34;)

      cell.textLabel?.text = tab.title

      return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      performSegue(withIdentifier: &#34;contentSegue&#34;, sender: self)
    }

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

      if (segue.identifier == &#34;contentSegue&#34;) {
            let nextVC = segue.destination as? ContentViewController
            let indexPath = self.tableview.indexPathForSelectedRow
            let tab = self.config[(indexPath?.row)!]
            nextVC?.tab = tab
      }
    }
}
</code></pre>

<p>其他几点说明:
1.我的小区标识符在IB中设置为'cell'<br/>
2. 我的 segue 在 IB 中设置为“show”,标识符为“contentSegue”<br/>
3. segue是从prototype cell到Content View Controller。<br/></p>

<p>我完全不知所措。任何人都可以帮忙吗?谢谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您确定将其设置为选择转场,而不是辅助 Action 吗?您可以通过 ctrl 单击 Storyboard中的单元格来检查这一点 - 确保您触发的 segue 是选择。</p>

<p>还有……</p>

<p>由于您在 Storyboard 中添加了 segue,因此无需在 <code>didSelectRowAt</code> 中触发它 - 因此您可以删除该函数。</p>

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

<pre><code>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
      nextVC.tab = tab
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UINavigationController 没有返回按钮,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46040245/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46040245/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UINavigationController 没有返回按钮