【问题标题】:Why ViewController back buttons don't work on Xcode?为什么 ViewController 后退按钮在 Xcode 上不起作用?
【发布时间】:2021-12-16 22:16:36
【问题描述】:

您好,我正在学习 Xcode 来制作 ios 应用程序。我有问题

这是错误代码 -> 线程 1:“-[UIViewController back2:]:无法识别的选择器发送到实例 0x14fb0e270”

第一个 ViewContorller 代码 ->

import UIKit

class ViewController: UIViewController {


    @IBAction func moveByNavi(_ sender: Any) {
        guard let uvc = self.storyboard?.instantiateViewController(withIdentifier: "SecondVC") else {
            return
        }
        
        self.navigationController?.pushViewController(uvc, animated: true)
    }
    
    @IBAction func movePresent(_ sender: Any) {
        guard let uvc = self.storyboard?.instantiateViewController(withIdentifier: "SecondVC") else {
            return
        }
        
        uvc.modalPresentationStyle = .fullScreen
        self.present(uvc, animated: true)
    }
}

第二个 ViewContorller 代码 ->

import UIKit

class SecondViewController: UIViewController {
    
    @IBAction func back(_ sender: Any) {
        self.presentingViewController?.dismiss(animated: true)
    }
    
    @IBAction func back2(_ sender: Any) {
        _ = self.navigationController?.popViewController(animated: true)
    }
}

每当我在第二个视图控制器上单击返回和返回 2 按钮时,我的模拟器就会停止。并在 appDelegate 上显示该错误代码。

我检查了第二个视图控制器的连接检查器。后退按钮连接良好。我也没有其他联系。 如何修复我的代码?

【问题讨论】:

    标签: ios swift iphone xcode


    【解决方案1】:

    我认为崩溃的原因是您连接了第二个后退按钮两次,您可以在情节提要的“显示连接检查器”(上方的右侧按钮)中进行检查。单击 Storyboard 中的按钮,然后单击“显示连接检查器”按钮,您将在“已发送事件”中看到连接

    【讨论】:

      【解决方案2】:

      确保您使用以下方法实例化视图控制器:

      storyboardInstance.instantiateViewController(withIdentifier: "myViewController")
      

      还要确保情节提要场景的身份检查器下的“情节提要 ID”值与您在代码中使用的值相同(“myViewController”)

      【讨论】:

      • 不确定我是否看到了相关性? VC 正在打开 - 导致问题的是关闭它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多