2011-10-04 3 views
0

프로그래밍 방식으로 UIButton을 만들었습니다. 단추를 처음으로 눌렀을 때 배경색을 올바르게 변경합니다. 그 후 버튼은 이후 터치 이벤트에 응답하지 않습니다.IOS의 UIButton은 첫 번째 터치에 응답 한 다음 후속 터치 이벤트에 응답하지 않습니다.

NSLog로 색상 변경 메소드를 구현했으며이 메소드는 첫 번째 터치 후에도 호출되지 않습니다. 단추가 시각적으로 iPhone 화면의 후속 조치에 반응하는 것처럼 보이더라도. 후속 터치가 실제로 어떤 이벤트 (예 : TouchDown)를 유발하지 않는 것 같지만, 이유를 파악할 수 없습니다.

나는이 문제와 관련하여 모든 게시물을 검색했지만 아무런 대답도 내 문제를 해결하지 못하는 것 같습니다.

버튼은 같은 헤더 파일에 선언 :

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

//Create initial view of Button One before any selection is made 

playerOneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
playerOneButton.frame = CGRectMake(5.0f, 20.0f, 149.0f, 31.0f); 

[playerOneButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15.0]]; 
[playerOneButton setTitle:playerOneName forState:UIControlStateNormal]; 

[self.view addSubview:playerOneButton]; 

//Create initial view of Button Two before any selection is made 

playerTwoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
playerTwoButton.frame = CGRectMake(166.0f, 20.0f, 149.0f, 31.0f); 

[playerTwoButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15.0]]; 
[playerTwoButton setTitle:playerTwoName forState:UIControlStateNormal]; 

[self.view addSubview:playerTwoButton]; 

// Add correct names to both buttons 

[playerOneButton setTitle:playerOneName forState:UIControlStateNormal]; 
[playerTwoButton setTitle:playerTwoName forState:UIControlStateNormal]; 

// Link buttons to methods that will toggle colors between Green for selected button and Red for unselected button 

[playerOneButton addTarget:self action:@selector(setPlayerOneButtonStatus) forControlEvents:UIControlEventTouchDown]; 

[playerTwoButton addTarget:self action:@selector(setPlayerTwoButtonStatus) 
      forControlEvents:UIControlEventTouchDown]; 

} 

실제로에 호출 방법 : 여기

UIButton *playerOneButton; 
UIButton *playerTwoButton; 

내가 UIViewController에로드 한 후 사용하는 버튼 초기화 코드는 색상을 변경하십시오.

-(void) setPlayerOneButtonStatus; 
{ 
playerOneButton.selected = YES; 

// Load colored images 
UIImage *imageGreen = [UIImage imageNamed:@"button_green.png"]; 
UIImage *imageRed = [UIImage imageNamed:@"button_red.png"]; 

// And create the stretchy versions. 
float wGreen = imageGreen.size.width/2, hGreen = imageGreen.size.height/2; 
UIImage *stretchGreen = [imageGreen stretchableImageWithLeftCapWidth:wGreen topCapHeight:hGreen]; 

float wRed = imageRed.size.width/2, hRed = imageRed.size.height/2; 
UIImage *stretchRed = [imageRed stretchableImageWithLeftCapWidth:wRed topCapHeight:hRed]; 

// Now create button One with Green background color 
playerOneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
playerOneButton.frame = CGRectMake(5.0f, 20.0f, 149.0f, 31.0f); 

[playerOneButton setBackgroundImage:stretchGreen forState:UIControlStateNormal]; 

[playerOneButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15.0]]; 
[playerOneButton setTitle:playerOneName forState:UIControlStateNormal]; 

[self.view addSubview:playerOneButton]; 

// Now create button Two with Red background color 

playerTwoButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
playerTwoButton.frame = CGRectMake(166.0f, 20.0f, 149.0f, 31.0f); 

[playerTwoButton setBackgroundImage:stretchRed forState:UIControlStateNormal]; 
[playerTwoButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15.0]]; 

[playerTwoButton setTitle:playerTwoName forState:UIControlStateNormal]; 

[self.view addSubview:playerTwoButton]; 

} 

답변

0

을 새로 만듭니다. 0을 대상으로 작업을 할당하지 않고 작업 메서드에서 호출합니다.

+0

내 문제가 완벽하게 해결되어 고맙습니다. – Peter

+0

새로 만든 버튼에 대상/액션을 추가하여 문제를 "해결"하지 않았 으면 좋겠습니다. 단추를 누를 때마다 새 인스턴스를 작성하지 않고 기존 인스턴스를 수정해야합니다. –

+0

설명 주셔서 감사합니다. 내가 제안한대로 문제를 해결했지만 모두 완벽하게 작동합니다. – Peter

관련 문제