开发者

Select A Picker leads to change of UIbutton Image

I have been searching method to build a UIpicker, which the row selected will lead to the change of the image of a UIbutton. The usual if, else method to change button seems does not work here. Does anyone knows how this can be done?

Here is the model code i found and i am using. How can i modify it?

Many Thanks.

@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 开发者_StackOverflowof selected color: %i", [arrayColors objectAtIndex:row], row);
}

@end


In the didSelectRow method you can check to see what index was selected. Then create your if statements from there. If that isnt working for some reason then I would create an NSTimer to check the value every 1 second. If the value of the picker changes, then change the image button? Thats the first thing that popped into my head.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜