iOS UIButton 点击无效
iOS UIButton click doesn't work
我对 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
点击应该用method
处理
- (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;
尽管如此,单击按钮不会触发方法调用。
这可能哪里有问题?
请试试这个。
[self.header addSubview:[self compareButton]];
希望对您有所帮助.....
//Better to Use Below lines
_comparebutton=[UIButton buttonwithtype:UIButtontyperoundrect];
[_comparebutton setframe:CGRectMake(0.0f, 20.0f, 100.0f, 44.0f)];
//instead of
//_compareButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 100.0f, 44.0f)];
1-您需要设置 header 视图的框架:
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 100.0f, 44.0f)];
2-所以在这种情况下,将按钮的框架设置为相对于 header 视图:
_compareButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 44.0f)];
3-不要忘记将 compareButton 属性 设置为强:
@property (nonatomic, strong) UIButton *compareButton;
我认为问题出在 self.header
。您需要为其设置框架。
self.header.frame = CGRectMake(0, 0, 320, 80);
在此之后你的按钮应该开始注册触摸。
为什么?
好问题。
由于您 self.header
的层有 属性 maskToBounds
默认设置为否,因此您在 header
视图中添加的任何视图都将可见。但是由于您的 header
视图没有可点击的区域,因此任何 subviews
都无法点击。这就是为什么你的按钮没有注册触摸。
修改你的代码如下
- (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@@@@@@@");
}
希望它能解决问题......!
我对 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
点击应该用method
- (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;
尽管如此,单击按钮不会触发方法调用。
这可能哪里有问题?
请试试这个。
[self.header addSubview:[self compareButton]];
希望对您有所帮助.....
//Better to Use Below lines
_comparebutton=[UIButton buttonwithtype:UIButtontyperoundrect];
[_comparebutton setframe:CGRectMake(0.0f, 20.0f, 100.0f, 44.0f)];
//instead of
//_compareButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 100.0f, 44.0f)];
1-您需要设置 header 视图的框架:
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 100.0f, 44.0f)];
2-所以在这种情况下,将按钮的框架设置为相对于 header 视图:
_compareButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 44.0f)];
3-不要忘记将 compareButton 属性 设置为强:
@property (nonatomic, strong) UIButton *compareButton;
我认为问题出在 self.header
。您需要为其设置框架。
self.header.frame = CGRectMake(0, 0, 320, 80);
在此之后你的按钮应该开始注册触摸。
为什么?
好问题。
由于您 self.header
的层有 属性 maskToBounds
默认设置为否,因此您在 header
视图中添加的任何视图都将可见。但是由于您的 header
视图没有可点击的区域,因此任何 subviews
都无法点击。这就是为什么你的按钮没有注册触摸。
修改你的代码如下
- (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@@@@@@@");
}
希望它能解决问题......!