簡體   English   中英

觸發UIButton動作時如何傳遞自定義對象?

[英]How can I pass a custom object when triggering a UIButton action?

我正在使用最新的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什么? 這是我可以用來傳遞此NSManagedObject的注釋。

看看如何在“ showDetails: ”方法中傳遞“ sender ”參數?

為什么不子類化“ UIButton ”(例如,將其命名為“ VansFannelButton ”),並給該新對象的“ @interface ”一個額外的ivar,它可以作為您的有效載荷。

然后,您可以執行以下操作:

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

使用Associated Objects passing more argument passingbuttons's object

請參閱關聯對象鏈接。

您可以嘗試添加屬性

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

我希望這有幫助..

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM