2011-06-12 109 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