繁体   English   中英

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点击应该由方法处理

- (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;

尽管如此,按钮单击不会触发方法调用。

哪个可能有问题呢?

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 - 不要忘记将compareButton属性设置为strong:

@property (nonatomic, strong) UIButton *compareButton;

我认为问题出在self.header 你需要设置框架。

self.header.frame = CGRectMake(0, 0, 320, 80);

在此之后,您的按钮应该开始注册触摸。

为什么?

好问题。

正如你自己一样self.header的图层有属性maskToBounds ,默认设置为no,因此你在header视图中添加的任何视图都是可见的。 但由于您的header视图没有可点击的区域,因此任何子subviews无法应用。 这就是为什么你的按钮没有注册触摸的原因。

请试试这个。

[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)];

修改您的代码如下

- (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@@@@@@@");
}

希望它能解决问题......!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM