2013-02-07 53 views
0

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

+0

你想添加到showDetails的什麼類型的參數? –

+0

@RajanBalana我爲我的問題添加了更多細節。它將是一個'NSManagedObject'。 – VansFannel

+0

有很多簡單的方法來做你想要的東西比子類化,標記(請不要)等。最好的是使用地圖視圖的方便內置的委託方法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

回答

1

看看您如何在「showDetails:」方法中傳遞「sender」參數?

爲什麼不繼承「UIButton」(例如命名爲「VansFannelButton‘),並給新對象的’@interface」獎金伊娃它可以是你的有效載荷。

然後,你可以這樣做:

- (void) showDetails: (VansFannelButton*) sender 
{ 
    if (sender) 
    { 
     // do something with the managed object payload 
     NSManagedObject * mObject = [sender payload]; 
    } 
} 
+0

謝謝。 '(id)sender是'ShopAnnotation'嗎? – VansFannel

+0

沒有。這是導致行動(或在這種情況下,方法)觸發的對象,這將是分類的「'UIButton'」a.k.a.「'VansFannelButton'」。 –

0

使用Associated Objectspassingmoreargumentbuttons'sobject

參考associated-objects鏈接。

+0

爲此使用關聯的對象是一種破解。爲什麼要在使用子類或按鈕標籤輕鬆定位更改時污染'UIButton'類? – zoul

0

你可以嘗試添加屬性

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動作,你可以做以下

- (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. 
} 

我希望這有助於..

感謝。

相關問題