2011-09-19 3 views
3

간단한 프로그램에서 버튼에 문제가 있습니다. 버튼은 뷰의 하위 뷰이며 뷰는 myViewController.view의 하위 뷰입니다.버튼 이벤트를 보낼 수있는 방법 - UIControlEventTouchUpInside

이 버튼은 동적으로 생성되지만 동작은 작동하지 않습니다. 어떻게 이벤트를 보낼 수 있습니까?

여기 내 코드가 있습니다.

//의 ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 

    bookCount = 0; 

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; 
     [self.view addSubview:subview]; 
     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [btn setFrame: subview.bounds]; 
     [btn addTarget:self action:@selector(onBtnchild:) forControlEvents:UIControlEventTouchUpInside]; 
     [subview addSubview:btn]; 
     ..... 
    } 
    return self; 

} 
-(void) onBtnchild:(id) sender{ 
    NSLog(@"HI"); 
} 

은 내 버튼을 볼 수 있지만 클릭 할 수 없습니다. 아이디어를 가르쳐주세요.

답변

1

코드는 다음과 같이해야한다 : 내가왔다

UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; 

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

[btn setFrame: subview.bounds]; 

[btn addTarget:self action:@selector(onBtnchild:) 
     forControlEvents:UIControlEventTouchUpInside]; 

[subview addSubview:btn]; 

[self.view addSubview:subview]; 
+0

코드를 흐른다. 그러나 나는 차이점을 발견 할 수 없다. 뭐가 문제 야? – bTagTiger

+0

그냥 붙여 넣기 및 답장 복사 –

+0

감사. 잘 작동합니다. 하지만 다른 문제가 있습니다. 버튼을 super로 추가 할 때 imageView를 추가 할 때. 작동하지 않습니다. – bTagTiger

관련 문제