我的iOS點擊處理有問題。iOS UIButton點擊不起作用
我有一個UIButton
:
- (UIButton *)compareButton
{
if (!_compareButton)
{
NSString *title = TITLE;
NSDictionary *attributes = TITLE_ATTRIBUTES;
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attributes];
_compareButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 100.0f, 44.0f)];
_compareButton.backgroundColor = [UIColor redColor];
[_compareButton setAttributedTitle:attributedTitle forState:UIControlStateNormal];
[_compareButton addTarget:self action:@selector(buttonCompareClicked:) forControlEvents:UIControlEventTouchUpInside];
}
return _compareButton;
}
UIButton
點擊應由方法
- (void)buttonCompareClicked:(UIButton *)sender
同樣被處理,我有一個UIView
:
- (UIView *)header
{
if (!_header)
{
UIView *header = [[UIView alloc] init];
header.backgroundColor = [UIColor whiteColor];
[self.view addSubview:header];
_header = header;
}
return _header;
}
我的按鈕的子視圖觀點:
[self.header addSubview:self.compareButton];
所有用戶的交互是有效的:
self.view.userInteractionEnabled = YES;
self.header.userInteractionEnabled = YES;
self.compareButton.userInteractionEnabled = YES;
儘管這樣,單擊按鈕不會觸發方法調用。
這可能是一個問題呢?
您是否設置了IBAction連接? – abhishekkharwar
你使用故事板或任何包含按鈕的XIB文件嗎? –
我看不到你在哪裏設置標題的框架,或添加約束來自動佈局佈局。它可能有零寬度和高度。嘗試設置它的'backgroundColor'屬性,以便您可以查看它是否具有有效大小。 – johnpatrickmorgan