簡體   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