我创建了一个按钮,就像下面的代码一样,但它不会在日志中响应(应该显示"ha").最奇怪的部分是我能够在集合视图中使用这个确切的代码,但通常不在viewcontroller上.按下按钮"myAction"我做错了什么?
-(void)viewDidLoad {
UIButton *buttonz = [UIButton buttonWithType:UIButtonTypeCustom];
buttonz.frame = CGRectMake(160, 0, 160, 30);
[buttonz setTitle:@"Charge" forState:UIControlStateNormal];
[buttonz addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[buttonz setEnabled:YES];
[buttonz setBackgroundColor:[UIColor blueColor]];
//add the button to the view
[backImageView addSubview:buttonz];
}
-(void)myAction:(id)sender {
NSLog(@"ha");
[self resignFirstResponder];
NSLog(@"ha");
}
Run Code Online (Sandbox Code Playgroud)