UITapGestureRecognizer在块中执行操作

时间:2014-08-26 14:09:35

标签: ios objective-c xcode

我有一个迭代来处理JSON提供的_id值,并为列表中的每个元素分配操作。我的问题是,当我点击元素之后,变量_id总是nil。有没有办法为每个抽头分配_id的当前迭代值?

int i=0;
for (NSDictionary *item in json) {
    NSString *_id = [item objectForKey:@"_id"];

    //Main container
    //
    UIView *temp_container = [[UIView alloc] init];
    temp_container.frame = CGRectMake(0, i*76, 320, 75);
    temp_container.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:[^{ NSLog(@"id value: %@", _id); } copy] action:@selector(invoke)];
    tap.numberOfTapsRequired = 1;
    [temp_container addGestureRecognizer:tap];
    i++;
}

这是控制台输出:

2014-08-26 18:17:32.511 appTwo[33519:60b] -[__NSArrayM invoke]: unrecognized selector sent to instance 0x91b6210
2014-08-26 18:17:32.515 appTwo[33519:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM invoke]: unrecognized selector sent to instance 0x91b6210'

0 个答案:

没有答案