我正在构建一个带有两个 UI 文本字段的应用程序。当触摸 UI 文本字段时,会出现一个 UI 选择器,供用户选择一个值以保留在 UI 文本字段中。当我测试应用程序并触摸一个 UI TextView 并滚动 UI Picker 时,两个 UI 文本字段都会更改值。这意味着,每个 UI 文本字段都受到每个 UI 选择器的影响。有谁知道为什么?谢谢你。
@interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
@end
@implementation ViewController
@synthesize ageTextField;
@synthesize relationshipTextField;
@synthesize resultLabel;
@synthesize calculateButton;
@synthesize agePickerView;
@synthesize relationshipPickerView;
@synthesize ageArray;
@synthesize relationshipArray;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//age pickerView
self.agePickerView = [[UIPickerView alloc]init];
self.agePickerView.delegate = self;
self.agePickerView.dataSource = self;
self.agePickerView.showsSelectionIndicator = YES;
self.ageTextField.inputView = self.agePickerView;
self.ageArray = @[ @"1", @"2", @"3", @"4", @"5", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12"
, @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23", @"24", @"25", @"26", @"27", @"28", @"29", @"30", @"31", @"32", @"33", @"34", @"35", @"36", @"37", @"38", @"39", @"40", @"41", @"42", @"43", @"44", @"45", @"46", @"47", @"48", @"49", @"50", @"51", @"52", @"53", @"54", @"55", @"56", @"57", @"58", @"59", @"60", @"61", @"62", @"63", @"64", @"65", @"66", @"67", @"68", @"69", @"70", @"71", @"72", @"73", @"74", @"75", @"76", @"77", @"78", @"79", @"80", @"81", @"82", @"83", @"84", @"85", @"86", @"87", @"88", @"89", @"90", @"91", @"92", @"93", @"94", @"95", @"96", @"97", @"98", @"99", @"100" ];
[self pickerView:self.agePickerView
didSelectRow:0
inComponent:0];
//relationship pickerView
self.relationshipPickerView = [[UIPickerView alloc]init];
self.relationshipPickerView.delegate = self;
self.relationshipPickerView.dataSource = self;
self.relationshipPickerView.showsSelectionIndicator = YES;
self.relationshipTextField.inputView = self.relationshipPickerView;
self.relationshipArray = @[ @"1", @"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13", @"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25", @"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37", @"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"50", @"51", @"52", @"53", @"54", @"55", @"56", @"57", @"58", @"59", @"60", @"61", @"62", @"63", @"64", @"65", @"66", @"67", @"68", @"69", @"70", @"71", @"72", @"73", @"74", @"75", @"76", @"77", @"78", @"79", @"80", @"81", @"82", @"83", @"84", @"85", @"86", @"87", @"88", @"89", @"90", @"91", @"92", @"93", @"94", @"95", @"96", @"97", @"98", @"99", @"100"];
[self pickerView:self.relationshipPickerView
didSelectRow:0
inComponent:0];
}
-(int) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(int) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.ageArray count];
return [self.relationshipArray count];
}
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [self.ageArray objectAtIndex:row];
return [self.relationshipArray objectAtIndex:row];
}
-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component{
self.ageTextField.text = [self.ageArray objectAtIndex:row];
self.relationshipTextField.text = [self.relationshipArray objectAtIndex:row];
}
- (void)viewDidUnload
{
[self setAgeTextField:nil];
[self setRelationshipTextField:nil];
[self setResultLabel:nil];
[self setCalculateButton:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end