2013-06-14 4 views
1

두 가지 상태의 간단한 UIButton이 있습니다. UIControlEventTouchDown 이벤트의 경우 button에 작업을 추가했습니다. button이 정상 상태 일 때 모두 효과적입니다. button을 터치하면 메소드가 호출됩니다. 그러나 buttonselected state에있을 때 button을 터치해도 아무 것도 나타나지 않습니다. 손가락을 떼면 상태가 정상 (예 : UIControlEventTouchUpInside)으로 바뀝니다.선택한 버튼의 UIControlEventTouchDown이 올바로 작동하지 않습니다.

어떻게 해결할 수 있습니까?

나는이 방법을 사용하여 내 버튼을 만듭니다

- (UIButton *) createButtonWithFrame:(CGRect)frame title:(NSString *)title { 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setFrame:frame]; 
    [button setTitle:title forState:UIControlStateNormal]; 
    [button setTitleColor:UIColorFromRGB(0x3a3a3a) forState:UIControlStateNormal]; 
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected]; 
    [[button titleLabel] setFont:[UIFont fontWithName:@"Gotham-Bold" size:10]]; 
    [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal]; 
    [button setBackgroundImage:[UIImage imageNamed:@"button_pressed.png"] forState:(UIControlStateSelected)]; 
    [button setAdjustsImageWhenHighlighted:NO]; 
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown]; 
    return button; 
} 

그리고 buttonPressed:이 같다 :

- (void) buttonPressed:(UIButton *)sender { 
    [sender setSelected:!sender.selected]; 
} 

답변

0

이 시도를

코드 프론트이 줄을 제거

[button setBackgroundImage:[UIImage imageNamed:@"button_pressed.png"] forState:(UIControlStateSelected)]; 

이 맘에 들지,

- (void) buttonPressed:(UIButton *)sender { 

    if([[sender backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"button.png"]]) 
     [sender setBackgroundImage:[UIImage imageNamed:@"button_pressed.png"] forState:UIControlStateNormal]; 
    else 
     [sender setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal]; 
} 
+0

추가하면 btn = (UIbutton *) 보낸 사람; –

+0

죄송합니다. 내 실수 ... 편집 된 답변을 확인하십시오. – Venkat

관련 문제