简体   繁体   English

iOS可能附加回调并关联其他发送者?

[英]iOS attaching callbacks and associating different sender possible?

Suppose I have a button that I am adding to an annotation object in a mapview: 假设我有一个按钮要添加到mapview的注释对象中:

 AnnotationButton* rightButton = [AnnotationButton buttonWithType:UIButtonTypeDetailDisclosure];
            [rightButton addTarget:self
                            action:@selector(showDetails:)
                  forControlEvents:UIControlEventTouchUpInside];

You will notice that the button calls the function showDetails when it is clicked. 您会注意到单击该按钮时会调用函数showDetails

Show details is defined as - (void)showDetails:(id)sender; 显示详细信息定义为- (void)showDetails:(id)sender; and takes a sender. 并接收一个发件人。 Is there a way to send more variables, or associate a different sender? 有没有办法发送更多变量,或关联其他发送者? The reason is that I want the button clicked to tell me which annotation is associated with that button. 原因是我希望单击按钮以告诉我与该按钮关联的注释。 Consider the annotation to be some other object which is available during the context where the button is created. 将注释视为创建按钮的上下文中可用的其他对象。

I thought about subclassing the UIButton class, and then storing additional information within it, but that seems like a hack. 我考虑过子类化UIButton类,然后在其中存储其他信息,但这似乎是一种hack。

Any ideas? 有任何想法吗?

If this button is being used for the rightCalloutAccessoryView or leftCalloutAccessoryView of a MKAnnotationView, your map's delegate should receive the message mapView:annotationView:calloutAccessoryControlTapped: when the button is tapped. 如果将此按钮用于rightCalloutAccessoryViewleftCalloutAccessoryView ,则当点击该按钮时,地图的委托应收到消息mapView:annotationView:calloutAccessoryControlTapped: This hands you the MKAnnotationView instance that was tapped, which has an annotation property to give you the corresponding annotation. 这将为您提供被点击的MKAnnotationView实例,该实例具有一个annotation属性,可为您提供相应的批注。 You should make use of that instead of trying to use an action on the button directly. 您应该利用它,而不是尝试直接在按钮上使用操作。

No, there is no way to change what is sent to the action message. 不,无法更改发送到操作消息的内容。 You can ask for two arguments, but they will be the button and the event that triggered it. 您可以要求两个参数,但是它们将是按钮和触发它的事件。 To get what you want, you have two options (that I can think of now). 要获得想要的东西,您有两个选择(我现在可以想到)。

  1. Use the button's tag property. 使用按钮的tag属性。 You can give each button a unique tag which identifiies the annotation, such as the index of the annotation in an array. 您可以为每个按钮提供一个唯一的标签,用于标识注释,例如数组中注释的索引。 Then it is easy to get the annotation in your showDetails: method. 然后,很容易在showDetails:方法中获取注释。
  2. Subclass UIButton. 子类UIButton。 There is nothing wrong with adding functionality to built in objects. 向内置对象添加功能没有错。 All you need to add is a property to hold some object. 您需要添加的只是一个保存某些对象的属性。 Bonus: If you use a generic id type for the property and give it a generic name, such as representedObject , you can use it in other projects in the future too. 奖励:如果你使用一个通用的id类型的属性,并给它一个通用名称,如representedObject ,您可以在将来使用它在其他项目了。
  3. from Anomie Use objc_setAssociatedObject to add a value to the buttons without subclassing. 来自Anomie的使用objc_setAssociatedObject可以向按钮添加一个值,而无需子类化。 You will probably want to add a category to UIButton to make it easier to use. 您可能需要向UIButton添加类别,以使其更易于使用。

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

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