gpt4 book ai didi

ios - 触发 UIButton 操作时如何传递自定义对象?

转载 作者:行者123 更新时间:2023-11-29 04:08:48 24 4
gpt4 key购买 nike

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

关于ios - 触发 UIButton 操作时如何传递自定义对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14749273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com