【问题标题】:How to perform a segue from a pushed vc that has the segue defined in the storyboard如何从在情节提要中定义了 segue 的推送 vc 执行 segue
【发布时间】:2019-09-17 15:35:28
【问题描述】:

事情是这样的:我的故事板是一个 SettingViewController(以 SVC 命名)和一个 StationsViewcontroller,后者通过“openDetails”segue 连接到细节视图控制器。

出于实际原因,我的 SVC 可以推送多个级别的具体设置视图控制器,我没有在情节提要中定义。

在某一时刻,其中一个 vc 推送了 StationsViewController,然后它可以向细节发出 performSegue:不幸的是,我得到“openDetails”segue 没有定义。

我应该怎么做才能解决这个问题?

--

代码没什么特别的,在SettingsViewController中,我推送其他VC,包括StationsViewController:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // Gracefully remove the selection
    tableView.deselectRow(at: indexPath, animated: true)

    guard let section = MainSections(rawValue: indexPath.section) else { return }

    switch section {
    case .stations:
        let vc = StationsViewController()
        // Do no segue at dismiss
        vc.onlyDismiss = true
        vc.didDismiss = {
            self.navigationController?.popViewController(animated: true)
        }
        push(vc, hideNavigation: true)

        // Blah blah

在我想要获取详细信息 vc 的 StationsViewController 中,我执行 segue:

func openPoiDetails(_ gs: GasStation) {

    poiDetails = gs

    performSegue(withIdentifier: "poiDetails", sender: nil)

    // Blah blah

故事板如下,我们可以看到左侧的 StationsViewController 通过“openDetails”segue 连接到 details vc:

【问题讨论】:

    标签: ios swift segue


    【解决方案1】:

    问题是您的 StationsViewController 只是从其初始化程序创建的,而 storyboard 对此一无所知。因此,您不能使用 segue。相反,您需要让storyboard 实例化StationsViewController

    替换:

    let vc = StationsViewController()
    

    与:

    let vc = self.storyboard?.instantiateViewController(withIdentifier: "StationsViewController") as! StationsViewController
    

    确保在 Xcode 的 Identify Inspector 中将 "StationsViewController" 设置为 Storyboard ID

    【讨论】:

    • ? 我觉得自己很可笑,我应该停止通宵工作,哈哈。谢谢。
    猜你喜欢
    • 2014-08-04
    • 1970-01-01
    • 2012-03-08
    • 2017-08-12
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2012-05-03
    相关资源
    最近更新 更多