2015-05-19 20 views
2

我的iOS點擊處理有問題。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

您是否設置了IBAction連接? – abhishekkharwar

+0

你使用故事板或任何包含按鈕的XIB文件嗎? –

+0

我看不到你在哪裏設置標題的框架,或添加約束來自動佈局佈局。它可能有零寬度和高度。嘗試設置它的'backgroundColor'屬性,以便您可以查看它是否具有有效大小。 – johnpatrickmorgan

回答

2

我認爲這個問題是self.header。你需要設置框架。

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

之後,您的按鈕應該開始註冊觸摸。

爲什麼?

好問題。

由於您的self.header的圖層具有屬性maskToBounds,它默認設置爲no,所以您在header視圖內添加的任何視圖都將可見。但由於您的header視圖沒有可用的區域,因此無法使用任何subviews。這就是爲什麼你的按鈕沒有註冊觸摸。

+0

非常感謝你的解答! –

+0

但是,如果標題視圖沒有區域,除非他設置了區域,子視圖如何顯示? –

+0

@BogdanPop閱讀解釋的第一句話。 – rptwsthi

1

請試試這個。

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

希望這可以幫助你.....

+0

爲什麼這應該有所幫助? –

+0

self.compareButton實際上調用[self compareButton],所以這不會改變任何東西 –

+0

它不會改變任何東西 –

1
//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)]; 
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 - 不要忘了在compareButton屬性設置爲強:

@property (nonatomic, strong) UIButton *compareButton; 
+0

請告訴我們,如果它的工作 –

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 [email protected]@@@@@@"); 
} 

希望它可以修復該問題....!