在objective-c下将执行选择器转换为SEL

时间:2013-07-29 20:09:04

标签: ios objective-c selector

我使用以下代码创建了UIButton

UIButton* queenButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
queenButton.frame = CGRectMake(0,50,30,30);
[queenButton setTitle:@"Q" forState:(UIControlState)UIControlStateNormal];
[queenButton addTarget:self action:(SEL)[self performSelector:@selector(promotePawnAt:to:) withObject:end withObject:@"Q"] forControlEvents:UIControlEventTouchUpInside];

当我这样做时,编译器会输出一个错误Cast of an Objective-C pointer to 'SEL' is disallowed with ARC。当我删除(SEL)广告时,我得到Implicit conversion of an Objective-C pointer to 'SEL' is disallowed with ARC

我想要做的是让程序在按下按钮时调用promotePawnAt:to:,但该函数需要参数,因此@selector(promotePawnAt:to:)不起作用。

1 个答案:

答案 0 :(得分:2)

你不能这样做。您必须使用一个参数创建一个方法,并将其添加为按钮的操作。

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

和内部调用yout方法:

-(void)action:(id)button {
[self promotePawnAt:end to:@"Q"];
}