我正在開發一個包含最新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