用数据夹在2个不同的NSViewController之间 [英] Swiching between 2 diferent NSViewControllers with data

查看:66
本文介绍了用数据夹在2个不同的NSViewController之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绝对是Swift和OSX开发的新手,如果我的问题太过老实了,我感到很抱歉.我想了解NSViewControllers之间的导航原理.

I'm absolute newbie in Swift and OSX development, and I'm sorry if my question will be too noob. I want to understand principles of navigation between NSViewControllers.

我有默认的ViewController,其中有登录名和密码字段以及用于登录的按钮.单击按钮后,返回令牌.现在,我尝试将视图"更改为SecondViewController并将其保存在某处,将来我将需要它.我该怎么做?可以在功能上做到这一点吗?:

I have my default ViewController, where are login and password fields and button to login. After click on button, returns token. And now I trying to change "view" to SecondViewController and save somewhere token, I will need it in future. How can I do it? and it possible to do this in function?:

@IBAction func loginButton(_ sender: Any) {
   .... 
}

谢谢!

推荐答案

您需要使用segues来执行此操作.

You need to use segues to perform this action.

首先要使用情节提要编辑器在ViewControllers之间建立segues连接.之后,您需要在情节提要的属性检查器上为segues提供一个标识符.然后,在您的代码中,可以像下面的代码一样通过segue调用新的ViewController.

First make the segues connections between the ViewControllers using to storyboard editor. After that you need to give the segues an identifier on the storyboard's attributes inspector. Then in your code you can call the new ViewController by the segue like the code below.

使用此代码,您可以使用按钮传递数据:

With this code you can pass the data using the button:

class ViewController: NSViewController {

    var dataToPass: String = "DataToPass"

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func loginButton(_ sender: Any) {

        performSegue(withIdentifier: NSStoryboardSegue.Identifier(rawValue: "segueIdentifier"), sender: self)
    }

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

        if segue.identifier!.rawValue == "segueIdentifier" {
            let destinationViewController = segue.destinationController as! ViewController2

            destinationViewController.dataToReceive = dataToPass
        }
    }
}


class ViewController2: NSViewController {

    var dataToReceive: String

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

使用此代码,您将使用override viewWillAppear

And with this code you will use the override viewWillAppear

class ViewController: NSViewController {

    var dataToPass: String = "DataToPass"

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewWillAppear() {

        performSegue(withIdentifier: NSStoryboardSegue.Identifier(rawValue: "segueIdentifier"), sender: self)
    }

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

        if segue.identifier!.rawValue == "segueIdentifier" {
            let destinationViewController = segue.destinationController as! ViewController2

            destinationViewController.dataToReceive = dataToPass
        }
    }
}


class ViewController2: NSViewController {

    var dataToReceive: String

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

在两种情况下,您都需要确保要传递给另一个视图控制器的数据不为空.

In both cases you need to assure the data you want to pass to the other view controller is not null.

这篇关于用数据夹在2个不同的NSViewController之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆