ios swift performSegueWithIdentifier 发送者值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32058668/
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 07:18:44  来源:igfitidea点击:

swift performSegueWithIdentifier sender value

iosswiftsegue

提问by Kiwo Tew

I am trying to understand how the sender value works in segues.

我试图了解发件人值如何在 segue 中工作。

In some places in my code both works:

在我的代码中的某些地方,两者都有效:

performSegueWithIdentifier("mySegue", sender: self)

performSegueWithIdentifier("mySegue", sender: sender)

But what is the difference between having self / sender?

但是拥有自我/发件人之间有什么区别?

回答by Duncan C

As @iosDev82 says in his answer, sender is an optional that names the object (if any) that triggered the segue.

正如@iosDev82 在他的回答中所说,sender 是一个可选的,它命名触发 segue 的对象(如果有的话)。

If you trigger a segue through code in a view controller, you could pass the view controller (self), or you could pass nil. It's just a piece of information that is passed along to prepareForSegue (again as iOSDv82 says.)

如果您通过视图控制器中的代码触发转场,您可以传递视图控制器(self),或者您可以传递 nil。这只是传递给 prepareForSegue 的一条信息(再次如 iOSDv82 所说。)

If you trigger a segue in the code of an IBAction method, your IBAction may have it's own sender parameter (frequently a button.) In that case you can pass along the sender parameter to the performSegueWithIdentifiermethod.

如果您在 IBAction 方法的代码中触发 segue,您的 IBAction 可能有它自己的 sender 参数(通常是一个按钮)。在这种情况下,您可以将 sender 参数传递给该performSegueWithIdentifier方法。

Example:

例子:

@IBAction func buttonAction(sender: UIButton)
{
  //In this case the button IBAction takes a pointer to the button as a param.
  //Pass it on to the segue in case performWithSegue needs it. 
  self.performSegueWithIdentifier("someID", sender: sender)
}

回答by Shamas S - Reinstate Monica

senderis just an argument that gets passed along with this function.

sender只是与此函数一起传递的参数。

This is received later in the function prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!), where you can get this object and make decision based on who the sender is.

这是稍后在函数中接收的prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!),您可以在其中获取此对象并根据发件人是谁做出决定。

回答by nyxee

As an Example, suppose you want to edit something at an IndexPath. You call

例如,假设您想在IndexPath编辑某些内容。你打电话

performSegue(withIdentifier: "showItem", sender: indexPath)

performSegue(withIdentifier: "showItem", sender: indexPath)

from wherever you want (indexPathis of type IndexPathhere).

无论你想(indexPath的类型是IndexPath这里)。

Then, inside the prepare(segue:sender:), you do:

然后,在 中prepare(segue:sender:),您执行以下操作:

...
switch segue.identifier {
case "showItem"?:
     // Figure out which row was just tapped
     if let row = tableView.indexPathForSelectedRow?.row {
         editRow(segue, row)
     } else {
         let selectedIndexPath = sender as! IndexPath
         editRow(segue, selectedIndexPath.row)
     }
...