我正试图点击一个按钮移动一个子视图但它崩溃并给我-[MainGameDisplay openTimeChanger:]: unrecognized selector sent to instance 0x10e443d0
这就是我正在做的事情:
@interface:
UIImageView *changerBackground;
@implementation
timerView = [[UIView alloc] initWithFrame:CGRectMake(0, 278, 105, 27)];
[self.view addSubview:timerView];
changerBackground = [[UIImageView alloc] init];
[changerBackground setImage:[UIImage imageNamed:@"speedbackground.png"]];
changerBackground.frame = CGRectMake(-12 , 9, 105, 33);
[changerBackground setUserInteractionEnabled:YES];
[timerView addSubview:changerBackground];
UIButton *timerBackground = [UIButton buttonWithType:UIButtonTypeCustom];
[timerBackground setBackgroundImage:[UIImage imageNamed:@"Time Background.png"] forState:UIControlStateNormal];
[timerBackground setBackgroundImage:[UIImage imageNamed:@"Time Background.png"] forState:UIControlStateHighlighted];
[timerBackground addTarget:self action:@selector(openTimeChanger:) forControlEvents:UIControlEventTouchUpInside];
timerBackground.frame = CGRectMake(-2 , 15, 102, 27);
[timerView addSubview:timerBackground];
-(void)openTimeChanger {
[UIView animateWithDuration:0.2f
animations:^{
changerBackground.frame = CGRectMake(-12 , -16, 105, 33);
}
completion:Nil];
}
还要注意,changerBackground有3个子视图。
答案 0 :(得分:1)
您需要更改此内容,
[timerBackground addTarget:self action:@selector(openTimeChanger:) forControlEvents:UIControlEventTouchUpInside];
到
[timerBackground addTarget:self action:@selector(openTimeChanger) forControlEvents:UIControlEventTouchUpInside]; //note that semicolon is not there in the method name
您的方法定义为-(void)openTimeChanger
而非-(void)openTimeChanger:(id)sender
。它试图找到一个具有这个定义的方法,但同样的方法不可用,因此崩溃。
如果您要使用[timerBackground addTarget:self action:@selector(openTimeChanger:) forControlEvents:UIControlEventTouchUpInside];
,请将方法更改为-(void)openTimeChanger:(id)sender
。