【问题标题】:How can I pass a custom object when triggering a UIButton action?触发 UIButton 操作时如何传递自定义对象?
【发布时间】:2013-02-07 10:49:30
【问题描述】:

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

【问题讨论】:

标签: ios objective-c cocoa-touch


【解决方案1】:

看看您是如何在“showDetails:”方法中传递“sender”参数的?

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

那么你可以这样做:

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

【讨论】:

  • 谢谢。 (id)senderShopAnnotation 吗?
  • 不。它是导致动作(或在本例中为方法)触发的对象,它将是子类“UIButton”又名“VansFannelButton”。
【解决方案2】:

Associated Objects 用于passing more argumentbuttons's object

参考associated-objects链接。

【讨论】:

  • 为此使用关联对象是一种 hack。当您可以使用子类或按钮的标记轻松本地化更改时,为什么要污染 UIButton 类?
【解决方案3】:

您可以尝试添加属性

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

我希望这会有所帮助..

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-03
    • 2015-04-18
    • 2020-03-21
    • 2023-03-19
    • 1970-01-01
    • 2018-08-28
    • 2014-08-12
    • 1970-01-01
    相关资源
    最近更新 更多