在下面的代码中,我定义了一个创建箭头(作为图像)的函数,并将这些箭头添加到视图中。但是,我收到了Undeclared selector 'method'
的警告。有人可以建议另一种方法来避免这种警告
-(void)createArrow:(NSString*)direction View:(UIView*)view
{
int x,y;
NSString *img;
NSString *method;
if ([direction isEqualToString:@"up"]){
x = 55;
y = 6;
_arrow = _arrowUp;
method = @"upTap";
img = @"up-arrow.png";
}else if ([direction isEqualToString:@"down"]){
x = 55;
y = 70;
_arrow = _arrowDown;
method = @"downTap";
img = @"down-arrow.png";
}else if ([direction isEqualToString:@"left"]){
x = 22;
y = 39;
_arrow = _arrowLeft;
method = @"leftTap";
img = @"left-arrow.png";
}else if ([direction isEqualToString:@"right"]){
x = 90;
y = 39;
_arrow = _arrowRight;
method = @"rightTap";
img = @"right-arrow.png";
}
_arrow = [[UIImageView alloc] initWithFrame: CGRectMake(x, y, 27, 27)];
[_arrow setImage:[UIImage imageNamed:img]];
UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(method)];
[_arrow setUserInteractionEnabled:YES];
[_arrow addGestureRecognizer:gest];
[view addSubview:_arrow];
}
答案 0 :(得分:1)
选择器名称以C字符串形式给出,而不是NSStrings。将method
更改为char *
并删除前导@
,或使用NSSelectorFromString(method)
。
另一种选择是将method
声明为SEL类型,并将其直接分配到if
语句的每个块中。
答案 1 :(得分:1)
Avi是对的。但更简单的是,为什么不只要method
类型为SEL
,并且在每种情况下,将其直接设置为要触发的选择器,然后将选择器传递给手势识别器?例如:
int x,y;
NSString *img;
SEL method;
if ([direction isEqualToString:@"up"]){
...
method = @selector(upTap);
...
} else if ([direction isEqualToString:@"down"]){
...
method = @selector(downTap);
...
} else if ([direction isEqualToString:@"left"]){
...
method = @selector(leftTap);
...
} else if ([direction isEqualToString:@"right"]){
...
method = @selector(rightTap);
...
}
...
UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:method];
...