0

我一直在寻找构建 UIpicker 的方法,选择的行会导致 UIbutton 的图像发生变化。通常的 if, else 更改按钮的方法似乎在这里不起作用。有谁知道如何做到这一点?

这是我找到并正在使用的模型代码。我该如何修改它?

非常感谢。

@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
4

1 回答 1

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 
                 }

    }



}
于 2011-06-12T17:37:16.630 回答