祝所有 iOS 开发者新年快乐。我想在 UITextfield
中显示血型。
所以我应该采用 UIPickerview 并附加血型列表。我的要求是,如果用户点击 UITextfiled 将显示 UIPickerview 并且用户选择血型,它将反射(reflect) UITextfield 中的血型项目。我正在尝试执行此功能。但是我在 UITextfield
中显示血型值时遇到了一些问题。问题是,例如,如果我单击 A-ve 血型,它将按 UITextfield
中的方式显示>.但是,如果我单击 A+ve 组,它将在 UITextfield
中显示为 A ve 而不是 A+ve。这是我的问题。请任何人帮助我。提前致谢。
下面给出的是我的代码。
//Bloodgroupviewcontroller.m
arrChooseServices=[[NSMutableArray alloc]initWithObjects:@"A+ve",@"A-ve",@"B+ve",@"B-ve",@"AB+ve",@"AB-ve",@"O+ve",@"O-ve", nil];
bloodGroup=[[UITextField alloc]initWithFrame:CGRectMake(150, 200, 120, 20)];
bloodGroup.borderStyle=UITextBorderStyleRoundedRect;
bloodGroup.backgroundColor=[UIColor clearColor];
bloodGroup.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
[bloodGroup setUserInteractionEnabled:NO];
bloodGroup.font=[UIFont systemFontOfSize:14.0];
bloodGroup.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
bloodGroup.textAlignment=NSTextAlignmentCenter;
bloodGroup.delegate=self;
bloodGroup.inputView=self.chooseServicePicker;
bloodGroup.inputAccessoryView=self.accessoryView;
[self.view addSubview:bloodGroup];
-(UIPickerView *)chooseServicePicker
{
if(chooseServicePicker==nil)
{
chooseServicePicker=[[UIPickerView alloc]init];
chooseServicePicker.delegate=self;
chooseServicePicker.dataSource=self;
chooseServicePicker.showsSelectionIndicator=YES;
}
return chooseServicePicker;
}
-(UIToolbar *)accessoryView
{
if ( accessoryView == nil )
{
accessoryView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onBloodGroupSelection)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
accessoryView.items=[NSArray arrayWithObjects:doneButton,cancelButton, nil];
}
return accessoryView;
}
- (void)onBloodGroupSelection
{
NSInteger row = [self.chooseServicePicker selectedRowInComponent:0];
bloodGroup.text=[arrChooseServices objectAtIndex:row];
[bloodGroup resignFirstResponder];
}
最佳答案
关于 +
未显示,我测试了您的代码,它对我来说工作正常。我必须将 [bloodGroup setUserInteractionEnabled:NO];
更改为 [bloodGroup setUserInteractionEnabled:YES];
以使 UITextField
可点击。不过,请尝试添加
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component{
bloodGroup.text = [arrChooseServices objectAtIndex:row] ;
}
添加到您的代码中,以便在点击 UIPickerView
时使 UITextField
更改文本。
关于ios - 如果用户单击 UIPickerview,如何在 UITextField 中显示血型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20876759/