ios prepareForSegue 和 PerformSegueWithIdentifier 发送者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37823521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:22:45  来源:igfitidea点击:

prepareForSegue and PerformSegueWithIdentifier sender

iosswiftiphonestoryboardsegue

提问by JasonP

I am wondering about how the functions in the title work and also about the sender parameter.

我想知道标题中的函数是如何工作的,还有关于发件人参数。

Lets say a button click calls the performSegue method, does that also call the prepareSegue method as well? Is the prepareSegue method called before the performSegue method but after the button is pressed?

假设单击按钮调用 performSegue 方法,那是否也调用 prepareSegue 方法?是否在 performSegue 方法之前但在按下按钮之后调用了 prepareSegue 方法?

Also, is the "sender" parameter in both of the functions linked? 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? 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?

另外,两个函数中的“sender”参数是否已链接?如果我在 performSegue 方法中传入一个字符串作为发送者,它会转移到 prepareSegue 方法中的发送者参数吗?换句话说,如果我将performSegue方法中的sender参数设置为“Hi world”,那么prepareSegue方法中的sender参数是否也是同一个字符串?

Thanks

谢谢

回答by Paulw11

There are, effectively, two ways you can trigger a segue. The first is via an action on a UI element in Interface Builder, the second is using performSegueWithIdentifier:sender:in your code. 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:

有两种有效的方法可以触发转场。第一个是通过 Interface Builder 中 UI 元素上的操作,第二个是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:函数。

In the case where the segue was initiated by an action on a UI element then the sender will be that UI element (i.e. if it is an action connection on a UIButtonthen the senderwill be the UIButtoninstance).

在其中SEGUE由UI元素上的动作发起则发送者将是UI元素(即,如果它是在一个动作连接的情况下UIButton,则sender将会是UIButton实例)。

If the segue is initiated by your code calling performSegueWithIdentifier:sender:then the senderwill be whatever object you passed as the sender. This could be your view controller, a button, an array, anything. So yes, if you pass "Hello World" to performSegueWithIdentifier:sender:as the sendervalue then this will be the senderin prepareForSegue:sender:

如果 segue 是由您的代码调用启动的,performSegueWithIdentifier:sender:那么sender将是您作为sender. 这可以是您的视图控制器、按钮、数组或任何东西。所以,是的,如果你传递的“Hello World”,以performSegueWithIdentifier:sender:作为sender值,那么这将是senderprepareForSegue:sender:

In terms of the order of operations:

从操作顺序来看:

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

回答by The_Curry_Man

The performSegue method calls a segue to be performed from one view to another. 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.

performSegue 方法调用要从一个视图到另一个视图执行的转场。在 segue 实际发生之前,会调用 prepareForSegue 方法,如果您想在视图之间传递数据,您可以在那里进行。

The performSegue method doesn't take the parameter you want to send. It's only used to call the segue in the first place. Any data that you want to send will be done through prepareForSegue.

performSegue 方法不接受您要发送的参数。它最初仅用于调用 segue。您要发送的任何数据都将通过 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!

让我知道这是否有帮助!

回答by Zion Perez

The_Curry_Man's answer worked for me. Here's an update of his code for Swift 3.

The_Curry_Man 的回答对我有用。这是他的 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
    }
}

回答by ingconti

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(...)
    }

}

回答by swiftBoy

Updated Method for Swift 5

Swift 5 的更新方法

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

Note : "showNextViewController" is identifier added for segue in storyboard

注意:“showNextViewController”是为故事板中的segue添加的标识符

回答by Nrv

while sending any object to the particular object to another view controller by using perform segue with an identifier, Please follow the steps #Swift4

通过使用带有标识符的执行转场将任何对象发送到特定对象到另一个视图控制器时,请按照步骤#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
    }
}