我想要實現的是:當我點擊一個特定的UIImageView時,UITapGesture會傳遞一個字符串到tap方法中。Objective-C:傳遞一個字符串參數來挑選@selector
我的代碼如下所示:想象一下,我有一個的UIImageView對象有不已,當我點擊這個圖片,就會打個電話,
UITapGestureRecognizer *tapFirstGuy = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(makeCallToThisPerson:@"1234567")];
[imageViewOne addGestureRecognizer:tapFirstGuy];
- (void)makeCallToThisPerson:(NSString *)phoneNumber
{
NSString *phoneNum = [NSString stringWithFormat:@"tel:%@", phoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNum]];
}
不過,我得到以下編譯錯誤:@selector(makeCallToThisPerson:@"1234567");
我無法確定發生了什麼。爲什麼我不能將字符串傳遞給私有方法?
唯一可以傳遞的是手勢識別器本身,就像其他目標動作方法一樣。 – rdelmar 2014-12-07 23:11:11
因爲選擇器換句話說是指向方法的指針,而不是指向閉包或塊。 – Andy 2014-12-07 23:11:46