[英]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. 如果将此按钮用于
rightCalloutAccessoryView
或leftCalloutAccessoryView
,则当点击该按钮时,地图的委托应收到消息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).
要获得想要的东西,您有两个选择(我现在可以想到)。
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. showDetails:
method. showDetails:
方法中获取注释。 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
,您可以在将来使用它在其他项目了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.