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;
尽管如此,按钮点击并不会触发方法调用。
这可能是个问题?
答案 0 :(得分:2)
1 - 您需要设置标题视图的框架:
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 100.0f, 44.0f)];
2 - 所以在这种情况下,将按钮的框架设置为相对于标题视图:
_compareButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 44.0f)];
3-Don忘记将compareButton属性设置为strong:
@property (nonatomic, strong) UIButton *compareButton;
答案 1 :(得分:2)
我认为问题出在self.header
上。你需要设置框架。
self.header.frame = CGRectMake(0, 0, 320, 80);
在此之后,您的按钮应该开始注册触摸。
<强> WHY吗
好问题。
由于self.header
的图层具有属性maskToBounds
,默认情况下设置为no,因此您在header
视图中添加的任何视图都将可见。但由于您的header
视图无法点按,因此任何subviews
都无法使用。这就是为什么你的按钮没有注册触摸的原因。
答案 2 :(得分:1)
请试试这个。
[self.header addSubview:[self compareButton]];
希望这可以帮助你......
答案 3 :(得分:1)
<div id="wrapper">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
答案 4 :(得分:1)
修改您的代码,如下所示
- (UIView *)header
{
if (!_header)
{
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 44.0f)];
header.backgroundColor = [UIColor whiteColor];
[header addSubView:[self compareButton]];//add this line of code
[self.view addSubview:header];
_header = header;
}
return _header;
}
//并修改
- (UIButton *)compareButton
{
if (!_compareButton)
{
NSString *title = TITLE;
NSDictionary *attributes = TITLE_ATTRIBUTES;
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attributes];
_compareButton=[UIButton buttonWithType:UIButtonTypeCustom];//modified
_compareButton.frame=CGRectMake(0.0f, 20.0f, 100.0f, 44.0f);//modified
_compareButton.backgroundColor = [UIColor redColor];
[_compareButton setAttributedTitle:attributedTitle forState:UIControlStateNormal];
[_compareButton addTarget:self action:@selector(buttonCompareClicked:) forControlEvents:UIControlEventTouchUpInside];
}
return _compareButton;
}
-(IBAction)buttonCompareClicked:(id)sender
{
NSLog(@"@@@@@@@@@Button Compare Called@@@@@@@");
}
希望它能解决问题....!