2011-06-12 86 views
0

我一直在尋找構建UIpicker的方法,所選行將導致UIbutton圖像的更改。通常的if,else方法改變按鈕似乎在這裏不起作用。有誰知道這可以做到嗎?選擇拾取器導致UIbutton圖像

這裏是我找到的模型代碼,我正在使用。我如何修改它?

很多謝謝。

@synthesize button; 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    arrayColors = [[NSMutableArray alloc] init]; 

    [arrayColors addObject:@"Orange"]; 
    [arrayColors addObject:@"Yellow"]; 
    [arrayColors addObject:@"Green"]; 
    [arrayColors addObject:@"Blue"]; 
    [arrayColors addObject:@"Indigo"]; 
    [arrayColors addObject:@"Violet"]; 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
    // Release anything that's not essential, such as cached data 
} 


- (void)dealloc { 
    [arrayColors release]; 
    [super dealloc]; 
} 

#pragma mark - 
#pragma mark Picker View Methods 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { 

    return 1; 
} 

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { 

    return [arrayColors count]; 
} 

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 

    return [arrayColors objectAtIndex:row]; 
} 

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 

    NSLog(@"Selected Color: %@. Index of selected color: %i", [arrayColors objectAtIndex:row], row); 
} 

@end 

回答

0

在didSelectRow方法中,您可以檢查以查看選擇了哪個索引。然後從那裏創建你的if語句。如果因爲某種原因沒有工作,那麼我會創建一個NSTimer來檢查每1秒的值。如果選取器的值發生變化,則更改圖像按鈕?那是我頭上的第一件事。

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 


    //component is the entire colomn on a picker "all of your colors" 
    if (component == 0) { 

       //row is the selected item in the colomn "orange" 
     if (row == 0) { 
       //color was selected 
        //change image here 
       } 

    } 



} 
+0

非常感謝。我現在會嘗試你的方法。 – Clarence 2011-06-12 17:50:58