简体   繁体   English

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

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

The error log like below错误日志如下

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)

There is a link for my demo detail https://drive.google.com/file/d/1LcY3hb3yGYf_3bZpo6qnvZPMviVp2Jft/view?usp=sharing我的演示详细信息https://drive.google.com/file/d/1LcY3hb3yGYf_3bZpo6qnvZPMviVp2Jft/view?usp=sharing有一个链接

I am a noob, please detail to say the step~ Thank you我是菜鸟,请详细说一下步骤~谢谢

Solution 1解决方案 1

You can't use pushViewController without navigationController .你不能在没有navigationController的情况下使用pushViewController
If you want you code will work, you need add navigationController to you ViewController .如果您希望您的代码能够正常工作,您需要将navigationController添加到您的ViewController中。
Click to your ViewController -> in upper menu select Editor -> choose Embed In -> Navigation Controller .单击您的ViewController -> 在上部菜单中 select Editor -> 选择Embed In -> Navigation Controller

在此处输入图像描述

Solution 2解决方案 2

Add Show segue to your NextViewController .Show segue 添加到您的NextViewController
在此处输入图像描述

Then rename to nextSegue and change kind to Present Modally and set presentation to Full Screen .然后重命名为nextSegue并将 kind 更改为Present Modally并将演示设置为Full Screen

在此处输入图像描述

In your ViewController :在您的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 name has module name as prefix, Make sure your ViewController and NextViewController both tick Inherit Module From Target with same module name in storyboard, giving the NextViewController a identifier call next , and you can do the following code. Swift class 名称以模块名称为前缀,确保您的ViewControllerNextViewController都勾选 Inherit Module From Target with same module name in storyboard,给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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM