Swift 从一个NSViewController移动到另一个NSViewController

Swift 从一个NSViewController移动到另一个NSViewController,swift,macos,nsviewcontroller,Swift,Macos,Nsviewcontroller,我对Mac应用程序开发完全陌生。在过去的两天里,我遇到了一个问题,但没有成功。 我的问题和你的问题一样 请您帮助我理解如何从一个NSViewController移动到另一个的过程和语法 我有一个用于登录的视图控制器,其中有两个字段,即UserId和password。 单击登录按钮时,将调用一个web API来验证用户身份,并在收到“SUCCESS”作为响应时,将控制权从LoginViewController转移到ProfileViewController 我曾试图解决这个问题,根据问题的答案(链

我对Mac应用程序开发完全陌生。在过去的两天里,我遇到了一个问题,但没有成功。 我的问题和你的问题一样

请您帮助我理解如何从一个
NSViewController
移动到另一个
的过程和语法

我有一个用于登录的视图控制器,其中有两个字段,即
UserId
password
。 单击登录按钮时,将调用一个web API来验证用户身份,并在收到
“SUCCESS”
作为响应时,将控制权从
LoginViewController
转移到
ProfileViewController


我曾试图解决这个问题,根据问题的答案(链接给出),但我得到了一个错误,即。“fromviewcontroller.view.superview不能为零。”

您必须在故事板中创建一个序列(Ctrl+左键单击LoginViewController上方的黄色圆圈按钮,并将其拖动到ProfileViewController),然后将其命名为“showProfile”

当您收到“成功”时,您要呼叫:

    //here you enter the name of the segue you want to call 
    //& in sender the data you want to pass to the new VC.
    performSegue(withIdentifier: "showProfile", sender: nil) 
这将呼叫

    prepare(for segue: UIStoryboardSegue, sender: Any?) 
在当前的ViewController中,因此如果要将数据传递给新的ViewController,则需要覆盖它

在ViewController之间传递数据的示例:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        //asking which segue is called
        if segue.identifier == "showProfile" {  

            //when you called "showProfile", you can be sure that your
            //destination is a ProfileViewController
            let vc = segue.destination as! ProfileViewController 

            //passing the data to your new VC
            vc.data = sender
        }
    }

创建一个从LoginViewController到ProfileViewController的序列,并为其指定一个标识符,如“showProfile”

不使用故事板

@IBAction func loginButton(_ sender: Any) {

    // Code for validating User.

    if response == "SUCCESS" {
        let profileController = ProfileViewController(nibName: "ProfileViewController", bundle: Bundle.main)
        profileController.data = responseData
        self.view.window!.contentViewController = profileController
    }

}

黄色圆圈和
UIStoryboardSegue
为iOS。问题是关于
NSViewController
和macOS的。可能重复是的,我在问题中提到了链接,但它对我不起作用。问题的正确答案充分解释了一切。你没有把你的问题放在一边,说明它不起作用,实际上你可能做错了什么。但是,除非你提供更多的信息,否则这个问题需要在IMHO中解决。
@IBAction func loginButton(_ sender: Any) {

    // Code for validating User.

    if response == "SUCCESS" {
        let profileController = ProfileViewController(nibName: "ProfileViewController", bundle: Bundle.main)
        profileController.data = responseData
        self.view.window!.contentViewController = profileController
    }

}