我创建了一个按钮,就像下面的代码一样,但它不会在日志中响应(应该显示"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)
1)
你不需要resignFirstResponder在按钮上" "(似乎我看到的大部分时间resignFirstResponder都是" "在文本字段或文本视图中).
2)
将控件事件从UIControlEventTouchUpInside更改为UIControlEventTouchDown
3)
父UIImageView可能没有userInteractionEnabled设置为true,这意味着事件不会被传播(或发送)到子视图.
要使这些事件可用于子视图,请通过以下方式更改父视图的userInteractionEnabled属性:
backImageView.userInteractionEnabled = YES;
Run Code Online (Sandbox Code Playgroud)
在添加该子视图之前.
| 归档时间: |
|
| 查看次数: |
6483 次 |
| 最近记录: |