使用TextField单击警报时,UIPickerView将更改为普通textField

时间:2016-04-06 12:06:51

标签: ios xcode uipickerview

我有一个文字字段说jobTextFiels,其inputView是UIPickerView,inputAccessoryView是UIToolBar。单击完成按钮(在工具栏中),如果从选择器中选择的行包含文本"其他",则会出现一个样式为plainTextStyle的警报。我的问题是,如果用户选择其他人'从选择器中单击警报上的添加按钮,然后为jobTextField显示正常键盘。我已经尝试过各种各样的事情,即尽可能地辞去这个文本域,但对我来说没有任何作用。

-(void)viewDidLoad
{
    pickerView = [[UIPickerView alloc] init];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.showsSelectionIndicator = YES;
    pickerView.backgroundColor=[UIColor whiteColor];
    _jobTypeTxtFld.inputView = pickerView;
    _genderTxtFld.inputView = pickerView;

    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    numberToolbar.barStyle = UIBarStyleDefault;
    numberToolbar.items = [NSArray arrayWithObjects:
                           [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                           nil];
    [numberToolbar sizeToFit];
    _jobTypeTxtFld.inputAccessoryView = numberToolbar;
    _genderTxtFld.inputAccessoryView = numberToolbar;
}

- (void)doneWithNumberPad{

    if (_jobTypeTxtFld.isFirstResponder) {
        if ([self.jobTypeTxtFld.text isEqualToString:@"Others"])
        {
            [UITextFieldAnimation moveContainerToInitialPositionWithView:self.bgView];
            jobAlert = [[UIAlertView alloc]initWithTitle:@"Please enter your job type" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add",nil];
            jobAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
            [jobAlert show];
        }
        else
        {
            [self.addrssTxtFld becomeFirstResponder];
        }
    }
    //[self.view endEditing:YES];
}

- (void)alertView:(UIAlertView *)alertview clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
    {
        case 0:
        {

        }
            break;

        case 1:
        {
            if (alertview == jobAlert) {
               // [_jobTypeTxtFld resignFirstResponder];
                [[jobAlert textFieldAtIndex:0] resignFirstResponder];
                if ([[jobAlert textFieldAtIndex:0].text isEqualToString:@""])
                {
                    [UITextFieldAnimation getAlertWithTitle:@"Message" andMessage:@"Please enter your job type!"];
                    [[jobAlert textFieldAtIndex:0] resignFirstResponder];
                }
                else
                {
                    self.jobTypeTxtFld.text = [jobAlert textFieldAtIndex:0].text;
                    [[jobAlert textFieldAtIndex:0] resignFirstResponder];
                }
                [_jobTypeTxtFld resignFirstResponder];
            }
        }
            break;
        default:
            break;
    }

#pragma mark
#pragma mark <UIPickerViewDataSource>
#pragma mark

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

#pragma mark - picker delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(_jobTypeTxtFld.isFirstResponder)
    {
        [self.jobTypeTxtFld setText:[jobTypeArr objectAtIndex:row]];

    }
    else if(_genderTxtFld.isFirstResponder)
    {
        NSArray *arr = @[@"Male",@"Female"];
        [self.genderTxtFld setText:[arr objectAtIndex:row]];
    }
}


// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    if(_jobTypeTxtFld.isFirstResponder)
    {
        return [jobTypeArr count];

    }
    else
    {
       return 2;
    }


}

// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if(_jobTypeTxtFld.isFirstResponder)
    {
        return [jobTypeArr objectAtIndex:row];

    }
    else
    {
        NSArray *arr = @[@"Male",@"Female"];
        return [arr objectAtIndex:row];
    }
}

// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    int sectionWidth = 300;

    return sectionWidth;
}

MVC

On Click of Resturant server

When Others is selected from picker and done button is clicked

On click of add of alerview button

on click of ok

1 个答案:

答案 0 :(得分:0)

点击完成按钮调用方法regisignFirstResponder。我希望这会对你有所帮助。