当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - How can i display the Blood group in UITextField if user clicks the UIPickerview

问题描述:

Wish you happy new year to all iOS developers.I want to display Blood group in UITextfield.

So I should took UIPickerview and append the Blood group list.My requirement is if user hit the UITextfiled the UIPickerview will be displayed and user select the blood group and it will reflect the blood group item in UITextfield.I am trying to do this functionality. But I have got some problem to display blood group value in UITextfield.The problem is for example if I click the A-ve blood group this will be displayed as it is in UITextfield.But if i click the A+ve group it will display like A ve instead of A+ve in UITextfield. This is my problem.Any body please help me. Thanks in advance.

The given below is my code.

//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];

}

网友答案:

About the + not displaying, I tested your code and it is working fine for me. I had to change the [bloodGroup setUserInteractionEnabled:NO]; to [bloodGroup setUserInteractionEnabled:YES]; to make the UITextField clickable. Still, try adding

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component{
     bloodGroup.text = [arrChooseServices objectAtIndex:row] ;
}

to your code to make the UITextField change text when the UIPickerView is tapped.

网友答案:

Try this, Add this method and other delegate method to ur class

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component
{
    yourtextfield.text = [yourdataarray objectAtIndex:row] ;
}
网友答案:
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];
 }
分享给朋友:
您可能感兴趣的文章:
随机阅读: