祝所有iOS开发者新年快乐。我想在UITextfield
中展示Blood group。
所以我应该拿UIPickerview
并追加血型组列表。我的要求是如果用户点击UITextfiled
将显示UIPickerview
并且用户选择血型并且它将反映血型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];
}
答案 0 :(得分:2)
关于+
没有显示,我测试了你的代码,它对我来说很好。我必须将[bloodGroup setUserInteractionEnabled:NO];
更改为[bloodGroup setUserInteractionEnabled:YES];
以使UITextField
可点击。不过,请尝试添加
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component{
bloodGroup.text = [arrChooseServices objectAtIndex:row] ;
}
点击您的代码,以便在点按UITextField
时进行UIPickerView
更改文字。
答案 1 :(得分:0)
试试这个, 将此方法和其他委托方法添加到您的类
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component
{
yourtextfield.text = [yourdataarray objectAtIndex:row] ;
}
答案 2 :(得分:0)
Take a string globally as "selectedString"
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
selectedString = yourdataarray[row];
}
-(void)onBloodGroupSelection
{
bloodGroup.text= selectedString;
[self.view endEditing:YES];
}