iphone - 选择器添加参数

标签 iphone objective-c ios ipad mapkit

我想做的基本上是用我的按钮发送某种形式的 ID,这样我就可以计算出哪个按钮被点击了。我的按钮是动态创建的,它们都执行加载新 View 的相同功能,我想知道单击了哪个按钮,以便我可以在新 View 上显示正确的数据。

我有一个 NSMutableArray 的注释,我在 pin 的详细信息中添加了一个按钮。该按钮有效并加载了下一个 View ,但我想弄清楚按下了哪个按钮。

我使用一个带有 NSMutableArray 的单例,称为数组。单例名称为Helper

我所做的是:

Helper* array = [Helper sharedManager];
int clickedNum = [array.array indexOfObject:annotation];
[myDetailButton setTag:clickedNum];
[myDetailButton addTarget:self action:@selector(moreInfo:event:) forControlEvents:UIControlEventTouchUpInside];

这就是我创建 map 注释的地方。下一行是我的函数ID

- (IBAction)moreInfo:(UIButton*)sender

这是我要检索标签的地方

Helper *array = [Helper sharedManager];
array.clicked = sender.tag;

当我运行它并单击我的一个注释中的按钮时,我得到一个异常,提示 NSInvalidArgumentException,原因:-(MapViewController moreInfo:event:]

我们将不胜感激

编辑:

根据 helper 接口(interface)的要求:

@interface Helper : NSObject {
    NSMutableArray *array;
    int clicked;
}
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic) int clicked;

+(id)sharedManager;

@end    

另外我认为添加是明智的:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{
static NSString *identifier = @"Events";
MKPinAnnotationView *retval = nil;
if ([annotation isKindOfClass:[Events class]]) 
{

    (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (retval == nil) {
        retval = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
        Helper *array = [Helper sharedManager];
        int clickedNum = [array.array indexOfObject:annotation];
        // Set up the Left callout
        UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
        myDetailButton.frame = CGRectMake(0, 0, 23, 23);
        myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [myDetailButton setTag:clickedNum];
        NSLog([NSString stringWithFormat:@"Testing tag: %@", clickedNum]);
        [myDetailButton addTarget:self action:@selector(moreInfo:) forControlEvents:UIControlEventTouchUpInside];
        // Set the image for the button
        [myDetailButton setImage:[UIImage imageNamed:@"icon72.png"] forState:UIControlStateNormal];

        // Set the button as the callout view
        retval.leftCalloutAccessoryView = myDetailButton;
    }


    if (retval) {
        [retval setPinColor:MKPinAnnotationColorGreen];
        retval.animatesDrop = YES;
        retval.canShowCallout = YES;
    }
}

return retval;   
}

最佳答案

您的方法 moreInfo: 与您在

中设置的选择器不匹配
[myDetailButton addTarget:self action:@selector(moreInfo:event:) forControlEvents:UIControlEventTouchUpInside];

所以把它改成

[myDetailButton addTarget:self action:@selector(moreInfo:) forControlEvents:UIControlEventTouchUpInside];

或者改变方法定义。

关于iphone - 选择器添加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6693163/

相关文章:

iphone - iOS 上与 Android 上的警报管理器工作方式相同的相应类是什么?

objective-c - 单个 UI 事件的多个 IBAction

iphone - 如何使用 didFinishPickingMediaWithInfo 从视频中查找 "Selected frames"的开始和结束持续时间?

ios - 在 UIView IOS 中按任意位置时移动带有触摸的 UIImageview

iOS10 语音识别器拼写

ios - 代码不在窗口层次结构 Root View 中

ios - 使用 UILocalNotifications、带有完成处理程序的远程通知和后台获取

iphone - 使用选项卡 View 启动应用程序但未选择选项卡?

ios - 应用程序名称未显示在 iTunes 中

iphone - 如何使用iPhone SDK直接加载矢量图像?