如果用户单击UIPickerview,如何在UITextField中显示Blood组

时间:2014-01-02 05:14:43

标签: ios iphone objective-c uipickerview

祝所有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];
 }

3 个答案:

答案 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];
 }