简体   繁体   English

prepareForSegue和PerformSegueWithIdentifier发件人

[英]prepareForSegue and PerformSegueWithIdentifier sender

I am wondering about how the functions in the title work and also about the sender parameter. 我想知道标题中的函数如何工作以及sender参数。

Lets say a button click calls the performSegue method, does that also call the prepareSegue method as well? 让我们说单击按钮会调用performSegue方法,这也会调用prepareSegue方法吗? Is the prepareSegue method called before the performSegue method but after the button is pressed? 是在performSegue方法之前调用prepareSegue方法但是在按下按钮之后?

Also, is the "sender" parameter in both of the functions linked? 另外,两个函数中的“sender”参数是否相关联? If I pass in a string as the sender in the performSegue method, will that transfer over to the sender parameter in the prepareSegue method? 如果我在performSegue方法中传入一个字符串作为发送者,那么是否会转移到prepareSegue方法中的sender参数? In other words, if I set the sender parameter in the performSegue method as "Hi world", will the sender parameter in the prepareSegue method also be the same string? 换句话说,如果我将performSegue方法中的sender参数设置为“Hi world”,那么prepareSegue方法中的sender参数是否也是相同的字符串?

Thanks 谢谢

There are, effectively, two ways you can trigger a segue. 实际上,有两种方法可以触发segue。 The first is via an action on a UI element in Interface Builder, the second is using performSegueWithIdentifier:sender: in your code. 第一个是通过Interface Builder中UI元素的操作,第二个是在代码中使用performSegueWithIdentifier:sender: I say 'effectively', because under the covers, when the scene is loaded from the storyboard, an action handler is configured that ultimately calls performSegueWithIdentifier:sender: 我说“有效”,因为在幕后,当从故事板加载场景时,配置一个动作处理程序,最终调用performSegueWithIdentifier:sender:

When performSegueWithIdentifier:sender: is called, the segue object is delivered to your view controller's prepareForSegue:sender: function. 当调用performSegueWithIdentifier:sender: ,segue对象将被传递给视图控制器的prepareForSegue:sender: function。

In the case where the segue was initiated by an action on a UI element then the sender will be that UI element (ie if it is an action connection on a UIButton then the sender will be the UIButton instance). 在通过UI元素上的动作启动segue的情况下,发送者将是该UI元素(即,如果它是UIButton上的动作连接,则sender将是UIButton实例)。

If the segue is initiated by your code calling performSegueWithIdentifier:sender: then the sender will be whatever object you passed as the sender . 如果您的代码调用performSegueWithIdentifier:sender:发起segue,则sender将是您作为sender传递的任何对象。 This could be your view controller, a button, an array, anything. 这可能是你的视图控制器,按钮,数组,任何东西。 So yes, if you pass "Hello World" to performSegueWithIdentifier:sender: as the sender value then this will be the sender in prepareForSegue:sender: 所以是的,如果你传递“Hello World”来执行performSegueWithIdentifier:sender:作为sender值,那么这将是prepareForSegue:sender:sender prepareForSegue:sender:

In terms of the order of operations: 在操作顺序方面:

  1. performSegueWithIdentifier:sender is called, either by your code or as a result of an action on a UI element performSegueWithIdentifier:sender由您的代码或UI元素上的操作调用
  2. If your view controller implements shouldPerformSegueWithIdentifier:sender: then this function is called. 如果视图控制器实现了shouldPerformSegueWithIdentifier:sender:则调用此函数。 If this function returns false then the segue is cancelled 如果此函数返回false则取消segue
  3. The segue object and destination view controller object are created 创建segue对象和目标视图控制器对象
  4. If your view controller implements prepareForSegue:sender: then this function is called. 如果视图控制器实现prepareForSegue:sender:则调用此函数。
  5. Once prepareForSegue:sender: returns, the segue completes. 一旦prepareForSegue:sender: ,segue就完成了。

The performSegue method calls a segue to be performed from one view to another. performSegue方法调用segue从一个视图到另一个视图。 Before the segue actually takes place, the prepareForSegue method is called, and if you want to pass data between the views, you'd do it there. 在segue实际发生之前,调用prepareForSegue方法,如果你想在视图之间传递数据,你可以在那里进行。

The performSegue method doesn't take the parameter you want to send. performSegue方法不接受您要发送的参数。 It's only used to call the segue in the first place. 它只用于首先调用segue。 Any data that you want to send will be done through prepareForSegue. 您要发送的任何数据都将通过prepareForSegue完成。

Here's an example. 这是一个例子。

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    performSegueWithIdentifier("test", sender: self)
    //You can set the identifier in the storyboard, by clicking on the segue
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "test"{
        var vc = segue.destinationViewController as! RandomViewController
        vc.data = "Data you want to pass"
        //Data has to be a variable name in your RandomViewController
    }
}

Let me know if this helps! 如果这有帮助,请告诉我!

The_Curry_Man's answer worked for me. The_Curry_Man的回答对我有用。 Here's an update of his code for Swift 3. 这是他的Swift 3代码的更新。

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

    performSegue(withIdentifier: "test", sender: self)
    //You can set the identifier in the storyboard, by clicking on the segue
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "test"{
        var vc = segue.destinationViewController as! RandomViewController
        vc.data = "Data you want to pass"
        //Data has to be a variable name in your RandomViewController
    }
}

my two cents for beginners... In swift 3 is: 我的初学者两分钱......在swift 3中是:

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

}

So, if arriving controller (of class MyController) implements a "fillData" method: 因此,如果到达的控制器(类MyController)实现了“fillData”方法:

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

    if let destController = segue.destination as MyController{

        destController.fillData(...)
    }

}

Updated Method for Swift 5 Swift 5的更新方法

performSegue(withIdentifier: "showNextViewController", sender: self)

Note : "showNextViewController" is identifier added for segue in storyboard 注意:“showNextViewController”是在storyboard中为segue添加的标识符

while sending any object to the particular object to another view controller by using perform segue with an identifier, Please follow the steps #Swift4 通过使用带有标识符的执行segue将任何对象发送到特定对象到另一个视图控制器,请按照步骤#Swift4

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "Detailed_Live_poll"{
        let destinationNavigationController = segue.destination as! UINavigationController
        let targetController = destinationNavigationController.topViewController as! NewLivePollViewController
        targetController.dictQuestInf = sender as! NSDictionary
    }
}

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

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