2013-02-07 47 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. 
} 

我希望这有助于..

感谢。

相关问题