2013-02-25 3 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' 루프에서 제어가 입력 되었습니까? –

+0

@AnoopVaidya 네, 루프에 들어갑니다. – jxdwinter

답변

9

이것은 아마도 터치 핸들링 이벤트 문제 일 수 있습니다. 나는. 접촉은 결코 당신에게 주문 단추에 얻고 있지 않다. 디버그하려면

  1. 버튼의 하위 클래스는 무엇입니까?
  2. userInterction을 사용하고 있습니까?
  3. 버튼의 수퍼 뷰를 확인하여 UIGestureRecognizer을 터치하면 터치 조작을 방해 할 수 있습니다.
  4. clipsToBounds 속성 아니요? 그렇다면보기 범위 밖의 감동입니까?
  5. 사용자 정의 버튼에서 touchesBegan을 재정 의하여 호출되는지 확인하십시오.
+0

커스텀 버튼이 UIButton으로 바뀌었지만 여전히 작동하지 않습니다 ... – jxdwinter

+0

다른 점은 어떻습니까? 슈퍼 뷰에 제스처 인식기를 추가 했습니까? touchesBegan을 재정의하고 중단 점을 설정하여 호출되는지 확인합니다. 정보가 많을수록 좋습니다. – Robert

0

방법 : (id)initWithFrame:(CGRect)frame은 PaiLifeCustomButton 유형 버튼을 만드는 데 사용되지 않습니다.

init을 호출 한 후 수동으로 프레임을 설정하십시오. 나는 당신의 코드를 많이 보았지만 괜찮아 보인다. 중단 점을 설정하여 코드가 실행되는지 여부를 확인하십시오.

1

이미 대상을 추가하고 싶었던 버튼의 수퍼 뷰 아래에 단추가있을 때 동일한 문제가있었습니다. 내 버튼 프레임을 변경했습니다. 문제가 해결되었습니다.

+0

이것이 내 문제였습니다. 그것은 작동했지만 충돌 상자는 작았습니다. –

0

동일한 문제가 발생했습니다. 문제는 내보기가 initWithFrame 대신 init으로 생성된다는 것입니다. Incase 누구나 같은 문제가 발생하면 도움이 될 것입니다.