我从另一个开发人员那里获得了一些代码,我试图把它放到正确的位置。其中一个问题是:
我有2个拣货员,它们填充了一个带有选择器中所选行的文本框。
如果选择了选择器1(名为:Type),则选择的行...在第3行时,默认为我的下一个选择器(名为:FlueType)上的那一行,当呈现选择器时。
如何让第二个选择器不受第一个选择器行选择的影响?
我看了[picker reloadComponent:0];
我已经看过堆栈,google似乎找不到适合这个问题的东西。
我已经在ViewDidLoad方法中尝试[picker selectRow:0 inComponent:0 animated:NO]
,希望它能将我的所有选择器设置为第一行值,这是我想要做的。
为代码之山道歉,我试图将其分解为上下文
/////////////////////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
答案 0 :(得分:0)
Ok sort ....已添加
[picker selectRow:0 inComponent:0 animated:NO]
;
显示选择器的按钮操作方法