简体   繁体   中英

iOS UIButton click doesn't work

I have an issue with iOS UIButton click handling programmatically.

I have a 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 clicks should be handled by method

- (void)buttonCompareClicked:(UIButton *)sender

Also, I have a UIView :

- (UIView *)header
{
    if (!_header)
    {
        UIView *header = [[UIView alloc] init];

        header.backgroundColor = [UIColor whiteColor];

        [self.view addSubview:header];

        _header = header;
    }

    return _header;
}

My button is a subview of a view:

[self.header addSubview:self.compareButton];

All user interactions are enabled:

self.view.userInteractionEnabled = YES;
self.header.userInteractionEnabled = YES;
self.compareButton.userInteractionEnabled = YES;

Despite of this, button click doesn't trigger method call.

Where might be a problem with this?

1-You need to set the frame of your header view:

UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 100.0f, 44.0f)];

2-So in this case, set the frame of your button relatively to the header view:

_compareButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 44.0f)];

3-Don't forget to set the compareButton property to strong:

@property (nonatomic, strong) UIButton *compareButton;

I think the problem is with self.header . You need to set frame to it.

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

After this that your button should start registering touch.

WHY?

Good Question.

As you self.header 's layer have property maskToBounds which by default set to no, so any view that you add inside header view will be visible. But since your header view have no area to tap on, that make any subviews untappable. That is why your button is not registering the touch.

Please try with this.

[self.header addSubview:[self compareButton]];

Hope this may help you.....

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

Modify your code as below

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

//AND modify

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

Hope it fixes the issue....!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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