1

I have Two text field in my View. i am using picker view as input. when ever picker is enabled the keyboard is visible behind the picker.The other issue is when i use Resign First responder for the text field in the Action the first time it shows picker without the keyboard at behind.But the second time when i click the text field the KeyBoard appears instead of picker. Here is my code.

- (IBAction)selectService:(id)sender
  {

  [self createActionSheet];

 //[selectServiceTextBox resignFirstResponder];

  pickerType = @"servicePickerType";

  servicePicker =  [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];

  pickerArray = [[NSArray alloc]initWithObjects:@"Money Transfer",@"Bill Payment", nil];

  servicePicker.dataSource = self;

  servicePicker.delegate = self;

  servicePicker.showsSelectionIndicator = YES;

  [actionSheet addSubview:servicePicker];

  // rowIndex = [stateTextField.text intValue];

  //[servicePicker selectRow:rowIndex inComponent:0 animated:NO];


 }

 - (IBAction)wayOfTransfer:(id)sender
  {   
     if ([selectedItem isEqualToString:@""])
    {

        NSLog(@"empty selection");
    }
    else if ([selectServiceTextBox.text isEqualToString:@"Money Transfer"])
    {

        [self createActionSheet];

        // [secondTextBox resignFirstResponder];

        //[selectServiceTextBox resignFirstResponder];

         pickerType = @"MoneyTransferMethod";

         servicePicker =  [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];

         pickerArrayTwo = [[NSArray alloc]initWithObjects:@"Cash Pick-up",@"Bank Account",@"Zym Card", nil];

         servicePicker.dataSource = self;

         servicePicker.delegate = self;

         servicePicker.showsSelectionIndicator = YES;

         [actionSheet addSubview:servicePicker];

    }  
 }
2
  • open pickerview at that time dismiss keyboard
    – Kirit Modi
    Commented Mar 26, 2014 at 11:22
  • i did that it works well for the first time. but wen i click again on the textbox. keyboard appears instead of picker view Commented Mar 26, 2014 at 11:23

2 Answers 2

1

You have to make the picker and keyboard appear exclusive of each other so that both can gracefully be switched between each other.. One way to do it is make the picker hidden by default and then bring it on screen when the first textview is on focus using an animation... Similarly hide it while bringing keyboard on using the second textview.

I have made a sample project that does this and have tested it... I hope this is what you intend to do...

//ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextViewDelegate,UIPickerViewDataSource,UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UITextView *firstTextView;
@property (weak, nonatomic) IBOutlet UITextView *secondTextView;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@end

//ViewController.m
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController{
    NSMutableArray *pickerDataSource;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    pickerDataSource = [[NSMutableArray alloc] initWithObjects:@"data1",@"data2", nil];

    self.pickerView.frame   =   CGRectMake(0, self.view.frame.size.height, 320, 162);

    self.pickerView.delegate    =   self;
    self.pickerView.dataSource  =   self;

}

// Textview delegates
-(void)textViewDidBeginEditing:(UITextView *)textView{
    if ([textView isEqual:self.firstTextView]) {
        [textView resignFirstResponder];

        if (self.pickerView.frame.origin.y >= self.view.frame.size.height) {
            [self showPicker];
        }else{
            [self hidePicker];
        }
    }else{
        [self hidePicker];
        [self.secondTextView becomeFirstResponder];


    }
}

-(void)showPicker{
    [UIView animateWithDuration:0.2 animations:^{
        self.pickerView.frame   =   CGRectMake(self.pickerView.frame.origin.x, self.pickerView.frame.origin.y - self.pickerView.frame.size.height, self.pickerView.frame.size.width, self.pickerView.frame.size.height);
    }];

}

-(void)hidePicker{
    [UIView animateWithDuration:0.2 animations:^{
        self.pickerView.frame   =   CGRectMake(self.pickerView.frame.origin.x, self.pickerView.frame.origin.y + self.pickerView.frame.size.height, self.pickerView.frame.size.width, self.pickerView.frame.size.height);
    }];
}

// Picker Delegates

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return pickerDataSource.count;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    self.firstTextView.text =   pickerDataSource[row];
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [pickerDataSource objectAtIndex:row];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Please let me know if there is anything unclear.

1

i think you need hide keyboard behind picker view ... simple you put this code in your text click action method UItextfield delegate method.....

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
 [text_box_variable resignFirstResponder];
}
3
  • this works for the first time, but the second time it shows keyboard rather then showing picker view Commented Mar 26, 2014 at 11:31
  • 1
    you need to put this code in uitextfield delegate method--> - (void)textFieldDidBeginEditing:(UITextField *)textField { [text_box_variable resignFirstResponder]; }
    – user3278923
    Commented Mar 26, 2014 at 12:10
  • now you can touch in text field multiple time ... i hope you help full this code..
    – user3278923
    Commented Mar 26, 2014 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.