-1

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:)不起作用。

4

1 回答 1

2

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

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

并在内部调用 yout 方法:

-(void)action:(id)button {
[self promotePawnAt:end to:@"Q"];
}
于 2013-07-29T20:12:00.590 回答