0

我正在使用最新的 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

4

3 回答 3

1

看看你是如何sender在你的“”方法中传递一个“”参数的showDetails:

为什么不将“ UIButton”子类化(例如将其命名为“ VansFannelButton”)并给新对象的“ @interface”一个额外的 ivar,它可以作为您的有效负载。

然后你可以这样做:

- (void) showDetails: (VansFannelButton*) sender
{
    if (sender)
    {
         // do something with the managed object payload
         NSManagedObject * mObject = [sender payload];
    }
}
于 2013-02-07T10:53:47.310 回答
0

用于Associated Objects_ passing more argument_buttons's object

请参阅关联对象链接。

于 2013-02-07T10:59:22.793 回答
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.
 }

我希望这有帮助..

谢谢。

于 2013-02-07T11:12:22.023 回答