我正在使用最新的 SDK 开发一个 iOS 应用程序。
我在以下方法上有一个选择器,我需要传递另一个参数:
- (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:
:
// 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];
这是如何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
如何添加另一个参数showDetails
以及如何传递它?
showDetails
将会:
- (void) showDetails:(id)sender shop:(NSManagedObject*)shop
而且,这是什么(id)sender
?这是我可以用来传递 this 的注释NSManagedObject
。