简体   繁体   English

如何基于segue执行动作

[英]How to perform actions based on segue

Here's what I'm trying to do: 这是我想做的事情:

I have 2 views, Settings and Main Screen. 我有2个视图,设置和主屏幕。

I've coded a segue to occur when a button in Settings is pressed, taking the view to the Main Screen. 我已经编码了在按下“设置”中的一个按钮时将场景切换到主屏幕的场景。 The identifier is "reset". 标识符为“重置”。

I'm trying to have the Main Screen perform a series of actions if this segue is triggered, but I can't find the function or way to do this. 如果尝试触发此segue,我试图让主屏幕执行一系列操作,但是我找不到执行此操作的功能或方式。

Any help on how to implement this? 对如何实施此方法有帮助吗? I'd like it to trigger when the segue occurs. 我希望它在触发segue时触发。

You can pass arguments to the main screen in the prepareForSegue function in the settings page. 您可以在设置页面的prepareForSegue函数中将参数传递到主屏幕。 Then in your main screen you can put in checks in your viewWillAppear function to handle them as you see fit. 然后,在主屏幕中,您可以在viewWillAppear函数中进行检查,以根据需要进行处理。

Example: 例:

In Settings: 在设置中:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "reset") {
        // pass data to next view
        let viewController:ViewController = segue!.destinationViewController as MainViewController
        viewController.settings = settings // where settings is what you want to pass
    }
}

In Main Screen: 在主屏幕中:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    if self.settings { // Do something }
}

If you're wanting something to happen when the segued view controller is triggered you can do it in a couple of places. 如果您希望触发定序视图控制器时发生某些事情,可以在几个地方进行。

If you want the event to happen at the time the segue is fired, perform your code inside of prepareForSegue . 如果您希望事件在触发segue时发生,请在prepareForSegue内部执行代码。 There you can check the UIStoryboardSegue object's identifier field for "reset" and if true, call your logic. 在那里,您可以检查UIStoryboardSegue对象的标识符字段是否存在“重置”,如果为true,则调用您的逻辑。

If you want it to happen when the destination view controller loads or appears, do it inside of its viewDidLoad or viewDidAppear methods. 如果希望在目标视图控制器加载或出现时发生这种情况,请在其viewDidLoadviewDidAppear方法内部进行操作。 In your case those methods would exist on the "Main Screen" view controller class. 在您的情况下,这些方法将存在于“主屏幕”视图控制器类中。

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

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