我正在开发一个包含最新SDK的iOS应用程序。如何在触发UIButton操作时传递自定义对象?
我有以下的方法选择和我需要通过另一种说法:
- (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
inte rface:
#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
?这是我可以用来通过这个NSManagedObject
的注释。
你想添加到showDetails的什么类型的参数? –
@RajanBalana我为我的问题添加了更多细节。它将是一个'NSManagedObject'。 – VansFannel
有很多简单的方法来做你想要的东西比子类化,标记(请不要)等。最好的是使用地图视图的方便内置的委托方法calloutAccessoryControlTapped。请参阅以下示例:http:// stackoverflow。com/questions/9462699 /如何识别哪个引脚被窃听,http://stackoverflow.com/questions/9797047/how-to-keep-data-associated-with-mkannotation-from-being-失去了一个callout-po后,http://stackoverflow.com/questions/9876042/annotation-details-after-detail-disclosure-pressed。 – Anna