Ios 触发UIButton操作时如何传递自定义对象?
我正在用最新的SDK开发一个iOS应用程序 我在以下方法上有一个选择器,需要传递另一个参数:Ios 触发UIButton操作时如何传递自定义对象?,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我正在用最新的SDK开发一个iOS应用程序 我在以下方法上有一个选择器,需要传递另一个参数: - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation { NSLog(@"viewForAnnotation"); MKAnnotationView *annotationView = nil; // if it's
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"viewForAnnotation");
MKAnnotationView *annotationView = nil;
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([annotation isKindOfClass:[ShopAnnotation class]])
{
// try to dequeue an existing pin view first
static NSString *ReusableAnnotationIdentifier = @"reusableShopAnnotationIdentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:ReusableAnnotationIdentifier];
if (!pinView)
{
// if an existing pin view was not available, create one
MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:ReusableAnnotationIdentifier];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
// add a detail disclosure button to the callout which will open a new view controller page
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
}
else
{
pinView.annotation = annotation;
}
annotationView = pinView;
}
return annotationView;
}
这就是showDetails
的实现方式:
- (void) showDetails:(id)sender
{
NSLog(@"Show Details");
DetailViewController* mvc = [[DetailViewController alloc] initWithNibName:@"DetailViewController_iPhone" bundle:nil];
mvc.delegate = self;
[self presentModalViewController:mvc animated:YES];
}
这是ShopAnnotation
界面:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import <CoreData/CoreData.h>
@interface ShopAnnotation : NSObject <MKAnnotation>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly, strong) NSString *title;
@property (nonatomic, readonly, strong) NSString *subtitle;
@property (nonatomic, readonly, strong) NSManagedObject* shop;
-(id)initWithCoordinate:(CLLocationCoordinate2D) c
title:(NSString *) t
subtitle:(NSString *) st
shop:(NSManagedObject*) shop;
@end
那么,这个(id)发送者是什么?这是一个注释,我可以用来传递此NSManagedObject
查看如何在“showDetails:
”方法中传递“sender
”参数
为什么不将“UIButton
”子类化(例如,将其命名为“VansFannelButton
”),并为新对象的“@接口
”提供一个额外的ivar,它可以作为您的有效负载
然后你可以做:
- (void) showDetails: (VansFannelButton*) sender
{
if (sender)
{
// do something with the managed object payload
NSManagedObject * mObject = [sender payload];
}
}
使用关联对象
进行传递
更多
参数
到按钮的
对象
请参阅链接。您可以尝试添加属性
int ID;
在ShopAnnotation类中,当您填充ShopAnnotation类时,您可以轻松设置自己的唯一ID
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (!pinView)
{
// if an existing pin view was not available, create one
MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:ReusableAnnotationIdentifier];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
// add a detail disclosure button to the callout which will open a new view controller page
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
//Set tag of the button same as ID
rightButton.tag=((ShopAnnotation*)annotation).ID;
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
}
}
我希望这有帮助
谢谢。你想在showDetails中添加什么类型的论点?@RajanBalana我已经在我的问题中添加了更多细节。这将是一个NSManagedObject
。有比子类化、标记(请不要)等更简单的方法来做您想要做的事情。最好的方法是使用地图视图方便的内置委托方法CalloutAccessoryControl。请参见以下示例:,。谢谢。(id)发送者是商店注释吗。它是导致操作(或者在本例中是方法)触发的对象,它将是子类“UIButton
”也称“VansFannelButton
”。使用关联对象进行此操作是一种攻击。当您可以使用子类或按钮的标记轻松地本地化更改时,为什么要污染UIButton
类?
int ID;
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (!pinView)
{
// if an existing pin view was not available, create one
MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:ReusableAnnotationIdentifier];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
// add a detail disclosure button to the callout which will open a new view controller page
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
//Set tag of the button same as ID
rightButton.tag=((ShopAnnotation*)annotation).ID;
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
}
}
- (void) showDetails:(id)sender
{
UIButton *btn=(UIButton*)sender;
ShopAnnotation *selectedAnnotation=nil;
for(ShopAnnotation *a in arrAnnotations){
if(a.ID == btn.tag){
selectedAnnotation=a; break;
}
}
// Use your ShopAnnotation for you further processing.
}