我从另一个开发人员那里获取了一些我试图纠正的代码。问题之一是:
我有 2 个选择器,它们用选择器中的选定行填充文本框。
如果选择了Picker 1(命名:type),则选择的行。SAYROW 3,在显示Picker的下一个Picker(命名:FlueType)上的该行默认为该行。
如何让第二个选择器不受第一个选择器行选择的影响?
我看过[picker reloadComponent:0];
我看过堆栈和谷歌,似乎找不到任何适合这个问题的东西。
我还尝试[picker selectRow:0 inComponent:0 animated:NO]
了 ViewDidLoad 方法,希望它将我所有的选择器设置为第一行值,这都是我想要做的。
为堆积如山的代码道歉,我试图将其分离到上下文中
/////////////////////picker action////////////////////////
-(void)flueTypeBtnClicked//:(id)sender
{
[flueTypeBtn.titleLabel setTextColor:[UIColor blackColor]];
currentSelectedIndex = flueTypeBtn.tag;
[makeTxt resignFirstResponder];
[modelTxt resignFirstResponder];
[locationTxt resignFirstResponder];
[oprPressureMBarTxt resignFirstResponder];
[picker setHidden:NO];
[picker reloadComponent:0];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
self.ToolsView.frame = CGRectMake(0, 200, 320, 260);
extern GasCertificateAppViewController *viewController;
[viewController.view addSubview:self.ToolsView];
[UIView commitAnimations];
}
-(void)typeBtnClicked//:(id)sender
{
[typeBtn.titleLabel setTextColor:[UIColor blackColor]];
currentSelectedIndex = typeBtn.tag;
[makeTxt resignFirstResponder];
[modelTxt resignFirstResponder];
[locationTxt resignFirstResponder];
[oprPressureMBarTxt resignFirstResponder];
[picker setHidden:NO];
[picker reloadComponent:0];
//NSLog(@"In type btn Clicked:");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
self.ToolsView.frame = CGRectMake(0, 200, 320, 260);
extern GasCertificateAppViewController *viewController;
[viewController.view addSubview:self.ToolsView];
[UIView commitAnimations];
///////////////////////////////////////////////////////////////////////////////////////////////////////////////more code here///////////////////////
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag == 1 )
{
[self typeBtnClicked];
return NO;
}
else if(textField.tag == 3)
{
[self flueTypeBtnClicked];
return NO;
}
CGPoint offset = CGPointMake(0, textField.frame.origin.y - textField.frame.size.height);
[self.screenScrollView setContentOffset: offset animated: YES];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
self.ToolsView.frame = CGRectMake(0, 200, 320, 260);
extern GasCertificateAppViewController *viewController;
[viewController.view addSubview:self.ToolsView];
[UIView commitAnimations];
return YES;
}
//////////////
-(void) fillData
{
extern Survey *survey;
ApplianceTest *test = [survey.applianceTestArray objectAtIndex:testId];
if(test.type != nil)
[self.typeTxt setText:test.type];
// [self.typeBtn setTitle:test.type forState:UIControlStateNormal];
if(test.make != nil)
[self.makeTxt setText:test.make];
if(test.model != nil)
[self.modelTxt setText:test.model];
if(test.location != nil)
[self.locationTxt setText:test.location];
if(test.flue_type != nil)
[self.flueTypeTxt setText:test.flue_type];
//[self.flueTypeBtn setTitle:test.flue_type forState:UIControlStateNormal]; if(test.operating_pressure_mbar_or_heat_input_kwh != nil)
[self.oprPressureMBarTxt setText:test.operating_pressure_mbar_or_heat_input_kwh];
if([test.safety_device_correct isEqualToString:@"Yes"])
[self.segtCtrl setSelectedSegmentIndex:0];
else
if([test.safety_device_correct isEqualToString:@"No"])
[self.segtCtrl setSelectedSegmentIndex:1];
else
[self.segtCtrl setSelectedSegmentIndex:2];
}
-(IBAction)bgTouched:(id)sender
{
CGPoint offset = CGPointMake(0, 0);
[self.screenScrollView setContentOffset: offset animated: YES];[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
self.ToolsView.frame = CGRectMake(0, 500, 320,260);
//[self.ToolsView removeFromSuperview];
//self.view.frame = CGRectMake(0, 0, 320, 372 );
[makeTxt resignFirstResponder];
[modelTxt resignFirstResponder];
[locationTxt resignFirstResponder];
[oprPressureMBarTxt resignFirstResponder];
[UIView commitAnimations];
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad {
[super viewDidLoad];
//segment control selected Index = 2;
segtCtrl.selectedSegmentIndex = 2;
navigationTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 0, 200, 25)];
navigationTitleLabel.textAlignment = UITextAlignmentCenter;
navigationTitleLabel.font= [UIFont fontWithName:@"Futura" size:18];
navigationTitleLabel.backgroundColor = [UIColor clearColor];
navigationTitleLabel.textColor = [UIColor whiteColor];
navigationTitleLabel.text = @"Appliance Test 1/3";
self.navigationItem.titleView = navigationTitleLabel;
self.screenScrollView.frame = CGRectMake(0, 0, 320, 370);
self.screenScrollView.contentSize = CGSizeMake(320, 600 + 200);
self.screenScrollView.clipsToBounds = YES;
self.screenScrollView.showsVerticalScrollIndicator = NO;
[self.view addSubview:self.screenScrollView];
testId = [testIdStr intValue];
currentSelectedIndex = 0;
typeArray = [[NSMutableArray alloc] init];
[typeArray addObject:@""]; ///change
[typeArray addObject:@"Boiler"];
[typeArray addObject:@"Cooker"];
[typeArray addObject:@"Fire"];
[typeArray addObject:@"Hob"]; ///change to hob
currentSelectedIndex = 0;
flueTypeArray = [[NSMutableArray alloc] init];
[flueTypeArray addObject:@""]; //change
[flueTypeArray addObject:@"OF"];
[flueTypeArray addObject:@"RS"];
[flueTypeArray addObject:@"FL"];
unitArray = [[NSMutableArray alloc] initWithObjects:@"MBar",@"KW/h",nil];
//self.navigationItem.title = @"Appliance Test 1/3";
//[typeTableView setHidden:YES];
//[flueTypeTableView setHidden:YES];
[segtCtrl setSelectedSegmentIndex:2];
extern Survey *survey;
[self fillData];
[picker setHidden:YES];
self.ToolsView.frame = CGRectMake(0, 500, 320, 260);
/////////////////
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
if (currentSelectedIndex == 1)
{
return [typeArray count];
}
else if(currentSelectedIndex == 3)
{
return [flueTypeArray count];
}
else if(currentSelectedIndex == 7)
{
return [unitArray count];
}
else
{
return 1;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (currentSelectedIndex == 1)
{
if ([typeArray count] == 0)
{
return [NSString stringWithFormat: @"None"];
}
else
{
return [typeArray objectAtIndex:row];
}
}
else if(currentSelectedIndex == 3)
{
if ([flueTypeArray count] == 0)
{
return [NSString stringWithFormat: @"None"];
}
else {
return [flueTypeArray objectAtIndex:row];
}
}
else if(currentSelectedIndex == 7)
{
if ([unitArray count] == 0)
{
return [NSString stringWithFormat: @"None"];
}
else
{
return [unitArray objectAtIndex:row];
}
}
return @"";
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// [gasSafeNoBtn setTitle:[NSString stringWithFormat:@"%d",[(NSNumber *) [gasSafeNoArray objectAtIndex:row]intValue]] forState:UIControlStateNormal];
extern Survey *survey;
if (currentSelectedIndex==1)
{
//typeTxt.text = [typeArray objectAtIndex:row];
[typeBtn.titleLabel setTextColor:[UIColor blackColor]];
[typeBtn setTitle:[typeArray objectAtIndex:row] forState:UIControlStateNormal];
typeTxt.text =[typeArray objectAtIndex:row] ;
}
else if(currentSelectedIndex == 3)
{
//flueTypeTxt.text = [flueTypeArray objectAtIndex:row];
[flueTypeBtn.titleLabel setTextColor:[UIColor blackColor]];
[flueTypeBtn setTitle:[flueTypeArray objectAtIndex:row] forState:UIControlStateNormal];
flueTypeTxt.text= [flueTypeArray objectAtIndex:row];
}
else if(currentSelectedIndex == 7)
{
unitString = [[NSString alloc] initWithString:[unitArray objectAtIndex:row]];
// NSLog(@"Unit String : %@", unitString);
[unitBtn setTitle:[unitArray objectAtIndex:row] forState:UIControlStateNormal];
}
else {
}
}
- (void)dealloc {
[unitArray release];
[ToolsView release];
[toolBar release];
[nextBtn release];
[prevBtn release];
[doneBtn release];
[picker release];
[super dealloc];
}
@end