我創建了一個按鈕,就像下面的代碼一樣,但它不會在日誌中響應(應該顯示「ha」)。最奇怪的部分是我已經能夠得到這個確切的代碼在集合視圖中工作,但通常不在視圖控制器上。我在做什麼錯按鈕按「myAction」?以編程方式在iOS上創建按鈕,但按下時沒有任何操作
-(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");
}
也不要忘記添加[super viewDidLoad]; in viewDidLoad – Yan