2011-10-04 101 views
0

我已經創建了一個UIButton編程按鈕時,觸摸向下首次爲希望正確地更改背景顏色的任何後續觸摸事件作出響應。之後,該按鈕不會響應任何後續的觸摸事件。的UIButton在IOS響應第一觸摸,然後不給

我已經儀表與NSLog的顏色變化的方法,該方法在第一觸摸後不叫,即使按鈕在視覺上顯得後續觸摸響應在iPhone屏幕上。看起來接下來的觸摸實際上並沒有觸發任何事件(例如TouchDown),但我不明白爲什麼不能。

我搜索過SOF相關的每一個崗位到這個問題,但沒有一個答案似乎解決我的問題。

按鈕在頭文件中聲明:

UIButton *playerOneButton; 
UIButton *playerTwoButton; 

這裏是我的UIViewController中加載後使用按鈕初始化代碼:

- (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]; 

} 

和被調用以實際的方法更改顏色:

-(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

您正在創建一個新的playerOneButton在操作方法中沒有爲其分配目標/操作。

+0

這完全解決了我的問題,我很感激! – Peter

+0

我希望通過將目標/行動到新創建的按鈕,你沒有「解決」你的問題。您必須修改現有實例,而不是在每次點擊該按鈕時都創建一個新實例。 –

+0

感謝您的澄清。我確實按照你的建議解決了這個問題,現在所有人都在完美地工作。 – Peter