• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UINavigationController 没有返回按钮

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:37:22 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

enter image description here

enter image description here 类 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中设置为'cell'
2. 我的 segue 在 IB 中设置为“show”,标识符为“contentSegue”
3. segue是从prototype cell到Content View Controller。

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



Best Answer-推荐答案


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

还有……

由于您在 Storyboard 中添加了 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
    }
}

关于ios - UINavigationController 没有返回按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46040245/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap