如何通过编写代码使用swift Viewcontroller打开objective-c Viewcontrollerv页面?

How can I use swift Viewcontroller through write coding to open the objective-c Viewcontrollerv page?

错误日志如下

2021-06-03 09:59:16.251029+0800 testing2[7167:2095191] [Storyboard] Unknown class _TtC8testing218NextViewController in Interface Builder file. Could not cast value of type 'UIViewController' (0x1d8c46428) to 'NextViewController' (0x102a90c50). 2021-06-03 09:59:16.252513+0800 testing2[7167:2095191] Could not cast value of type 'UIViewController' (0x1d8c46428) to 'NextViewController' (0x102a90c50). (lldb)

我的演示详细信息有一个 link https://drive.google.com/file/d/1LcY3hb3yGYf_3bZpo6qnvZPMviVp2Jft/view?usp=sharing

我是菜鸟,请详细说一下步骤~谢谢

解决方案 1

没有 navigationController 就不能使用 pushViewController
如果你希望你的代码可以工作,你需要添加 navigationController 到你 ViewController.
单击您的 ViewController -> 在上方菜单 select Editor -> 选择 Embed In -> Navigation Controller.

解决方案 2

Show segue 添加到您的 NextViewController

然后重命名为 nextSegue 并将类型更改为 Present Modally 并将演示文稿设置为 Full Screen

在你的 ViewController:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func btn_next(_ sender: UIButton) {
        performSegue(withIdentifier: "nextSegue", sender: nil)
    }
    

}

Swift class 名称有模块名称作为前缀,确保你的 ViewControllerNextViewController 都勾选 Inherit Module From Target with same module name in storyboard, giving NextViewController 标识符调用 next,您可以执行以下代码。

@IBAction func btn_next(_ sender: UIButton) {
    
    guard let next = self.storyboard?.instantiateViewController(identifier: "next") as? NextViewController else {
        print("next is uninitialized")
        return
    }
    self.present(next, animated: true, completion: nil)
}