prepareForSegue和PerformSegueWithIdentifier发件人 [英] prepareForSegue and PerformSegueWithIdentifier sender

查看:90
本文介绍了prepareForSegue和PerformSegueWithIdentifier发件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道标题中的函数如何工作以及sender参数。

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

让我们说按钮点击会调用performSegue方法,这也会调用prepareSegue方法吗?是否在performSegue方法之前调用prepareSegue方法但是在按下按钮之后?

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?

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

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?

谢谢

推荐答案

实际上有两种方法可以触发一个segue。第一个是通过Interface Builder中的UI元素执行操作,第二个是在代码中使用 performSegueWithIdentifier:sender:。我说'有效',因为在幕后,当从故事板加载场景时,配置一个动作处理程序,最终调用 performSegueWithIdentifier:sender:

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:

当调用 performSegueWithIdentifier:sender:时,segue对象将被传递给视图控制器的 prepareForSegue:sender: function。

When performSegueWithIdentifier:sender: is called, the segue object is delivered to your view controller's prepareForSegue:sender: function.

如果segue是由UI元素上的动作启动的,则发送者将是该UI元素(即,它是 UIButton 上的操作连接,然后发件人将是 UIButton instance)。

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 UIButton then the sender will be the UIButton instance).

如果您的代码调用 performSegueWithIdentifier:sender:启动segue,那么 sender 将是您作为 sender 传递的任何对象。这可能是你的视图控制器,按钮,数组,任何东西。所以,是的,如果您将Hello World传递给 performSegueWithIdentifier:sender:作为 sender 值,那么这将是发件人 in prepareForSegue:sender:

If the segue is initiated by your code calling performSegueWithIdentifier:sender: then the sender will 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 sender value then this will be the sender in prepareForSegue:sender:

就...而言操作顺序:

In terms of the order of operations:


  1. performSegueWithIdentifier:sender 由您的代码调用或者作为对UI元素的操作的结果

  2. 如果您的视图控制器实现 shouldPerformSegueWithIdentifier:sender:,则调用此函数。如果此函数返回 false ,则取消segue

  3. 创建segue对象和目标视图控制器对象

  4. 如果您的视图控制器实现 prepareForSegue:sender:,则调用此函数。

  5. 一次 prepareForSegue:sender:返回,segue完成。

  1. performSegueWithIdentifier:sender is 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 false then 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.

这篇关于prepareForSegue和PerformSegueWithIdentifier发件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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