2013-02-25 108 views
3

的ViewController:的UIButton addTarget不起作用

@implementation PaiLifeViewController 
@synthesize detail = _detail; 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    UIImage *i = [UIImage imageNamed:@"menu_patcode_normal"]; 
    UIButton *c1 = [[UIButton alloc] init]; 
    c1.tag = 11; 
    UIButton *c2 = [[UIButton alloc] init]; 
    c2.tag = 12; 
    UIButton *c3 = [[UIButton alloc] init]; 
    c3.tag = 13; 
    UIButton *c4 = [[UIButton alloc] init]; 
    c4.tag = 21; 
    UIButton *c5 = [[UIButton alloc] init]; 
    c5.tag = 22; 
    UIButton *c6 = [[UIButton alloc] init]; 
    c6.tag = 23; 
    UIButton *c7 = [[UIButton alloc] init]; 
    c7.tag = 31; 
    UIButton *c8 = [[UIButton alloc] init]; 
    c8.tag = 32; 
    UIButton *c9 = [[UIButton alloc] init]; 
    c9.tag = 33; 

    [c1 setBackgroundImage:i forState:UIControlStateNormal]; 
    [c2 setBackgroundImage:i forState:UIControlStateNormal]; 
    [c3 setBackgroundImage:i forState:UIControlStateNormal]; 
    [c4 setBackgroundImage:i forState:UIControlStateNormal]; 
    [c5 setBackgroundImage:i forState:UIControlStateNormal]; 
    [c6 setBackgroundImage:i forState:UIControlStateNormal]; 
    [c7 setBackgroundImage:i forState:UIControlStateNormal]; 
    [c8 setBackgroundImage:i forState:UIControlStateNormal]; 
    [c9 setBackgroundImage:i forState:UIControlStateNormal]; 

    NSArray *c = [[NSArray alloc] initWithObjects:c1,c2,c3,c4,c5,c6,c7,c8,c9, nil]; 
    _detail= [[PaiLifePageDetail alloc] initWithDataSource:c pageIndex:1]; 
    for (UIButton *b in c) 
    { 
     [b addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    [self.view addSubview:_detail]; 
} 

- (void) clicked : (UIButton *) button 
{ 
    NSLog(@"button = %i", button.tag); 
} 

@end 

自定義視圖:

@implementation PaiLifePageDetail 
#define MARGINTOTOP 120.0 
#define MARGINTOLEFT 20.0 
#define WIDTH 80.0 
#define HEIGHT 60.0 

- (id) initWithDataSource : (NSArray *) dataSource pageIndex : (int) pageIndex; 
{ 
    self = [super init]; 
    if (self) 
    { 
     if (dataSource) 
     { 
      for (UIButton *button in dataSource) 
      { 
       //page 1 line 1 
       if (pageIndex == 1) 
       { 
        if (button.tag == 11) 
        { 
         button.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP, WIDTH * 2 + 20, HEIGHT); 
         [self addSubview:button]; 
        } 
        else if (button.tag == 12) 
        { 
         button.frame = CGRectMake(MARGINTOLEFT + WIDTH * 2 + 20 * 2, MARGINTOTOP, WIDTH, HEIGHT); 
         [self addSubview:button]; 
        } 
       } 
       //not page 1 line 1 
       else 
       { 
        if (button.tag == 11) 
        { 
         button.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP, WIDTH, HEIGHT); 
         [self addSubview:button]; 
        } 
        else if (button.tag == 12) 
        { 
         button.frame = CGRectMake(MARGINTOLEFT + WIDTH + 20, MARGINTOTOP, WIDTH, HEIGHT); 
         [self addSubview:button]; 
        } 
        else if (button.tag == 13) 
        { 
         button.frame = CGRectMake(MARGINTOLEFT + (WIDTH + 20) * 2, MARGINTOTOP, WIDTH, HEIGHT); 
         [self addSubview:button]; 
        } 
       } 

       // line 2 
       if (button.tag == 21) 
       { 
        button.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP + HEIGHT + 20, WIDTH, HEIGHT); 
        [self addSubview:button]; 
       } 
       else if (button.tag == 22) 
       { 
        button.frame = CGRectMake(MARGINTOLEFT + WIDTH + 20, MARGINTOTOP + HEIGHT + 20, WIDTH, HEIGHT); 
        [self addSubview:button]; 
       } 
       else if (button.tag == 23) 
       { 
        button.frame = CGRectMake(MARGINTOLEFT + (WIDTH + 20) * 2, MARGINTOTOP + HEIGHT + 20, WIDTH, HEIGHT); 
        [self addSubview:button]; 
       } 

       // line 3 
       if (button.tag == 31) 
       { 
        button.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP + (HEIGHT + 20) * 2, WIDTH, HEIGHT); 
        [self addSubview:button]; 
       } 
       else if (button.tag == 32) 
       { 
        button.frame = CGRectMake(MARGINTOLEFT + WIDTH + 20, MARGINTOTOP + (HEIGHT + 20) * 2, WIDTH, HEIGHT); 
        [self addSubview:button]; 
       } 
       else if (button.tag == 33) 
       { 
        button.frame = CGRectMake(MARGINTOLEFT + (WIDTH + 20) * 2, MARGINTOTOP + (HEIGHT + 20) * 2, WIDTH, HEIGHT); 
        [self addSubview:button]; 
       } 
      } 
     } 
    } 
    return self; 
} 

@end 

enter image description here

enter image description here

但是當我點擊這些按鈕,他們沒有在所有的工作,請幫助我,非常感謝。

+0

你有檢查與斷點,控制'for'循環進入? – 2013-02-25 16:34:57

+0

@AnoopVaidya是的,它進入循環 – jxdwinter 2013-02-25 16:39:22

回答

9

這可能是一個觸摸handeling事件問題。即觸摸永遠不會讓你自定義按鈕。要調試此:

  1. 什麼是您的按鈕的子類?
  2. 檢查userInterction是否啓用?
  3. 檢查您的按鈕的超級瀏覽UIGestureRecognizer - 他們可以干擾觸摸handeling。
  4. 你是clipsToBounds屬性沒有?如果是這樣的話,觸及視野範圍之外的情況呢?
  5. 覆蓋自定義按鈕中的touchesBegan以查看它是否被調用。
+0

我改變了自定義按鈕到UIButton,但仍然沒有工作... – jxdwinter 2013-02-26 03:43:46

+0

其他點呢?你有沒有給superviews添加任何手勢識別器?覆蓋touchesBegan並設置一個斷點以查看它是否被調用。越多的信息越好。 – Robert 2013-02-26 09:49:46

0

方法:(id)initWithFrame:(CGRect)frame不用於創建那些PaiLifeCustomButton類型的按鈕。

或者在調用init之後手動設置框架。我的代碼看起來很多,但它看起來很好。設置斷點以查看代碼是否運行。

1

我有同樣的問題,當我已經有按鈕在我的按鈕的超級視圖下面,爲此我想添加目標。我改變了我的按鈕框架,然後問題解決了

+0

這是我的問題。它正在工作,但碰撞盒很小。 – 2016-03-17 10:43:03

0

我有同樣的問題,問題是我的視圖是用init而不是initWithFrame創建的。 Incase任何人都有同樣的問題,那麼它可以幫助你。