2010-04-07 6 views
0
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 180)]; 
myLabel.backgroundColor = [UIColor greenColor]; 
[self.view addSubview:myLabel]; 

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[self addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchDown]; 
myButton.frame = CGRectMake(80.0, 120.0, 30, 30); 
myButton.showsTouchWhenHighlighted = YES; 
[self addSubview:myButton]; 

작동 버튼 작동 (show: 전화),하지만 버튼이 작동하지 않습니다 [myLabel addSubview:myButton];을 사용할 수 없습니다. 이유가 확실하지 않은가요?있는 UIButton이 방법으로

---------------- 편집 & 솔루션은 ------------ 기본적으로 감사 @KennyTM

UILabel의 취급하지 않습니다 모든 사건. 라벨의 userInteractionEnabled 속성을 YES으로 설정해야합니다.

또한 UILabel 상단에 UIButton을 추가하지 않는 것이 좋습니다.

답변

1

기본적으로 UILabel은 모든 이벤트를 처리하지 않습니다. 라벨의 userInteractionEnabled property을 YES로 설정해야합니다.

버튼은 어쨌든 라벨의 하위보기가 아니어야합니다. 비논리적입니다. 두 가지 모두 UIView의 하위보기로 만듭니다.

+0

그냥 UIView의 모든 하위 클래스라고 생각해 주셔서 감사합니다. –

+0

@william : UILabel *은 UIView의 하위 클래스이며, 명시 적으로 'userInteractionEnabled'를 명시 적으로 해제합니다. – kennytm