1

I have a form-like view in my application which contains 8 textfields. Two of them are to be filled in by pickers, and the rest by normal heyboard typing. When picker-row is selected or when return button is pressed, the corresponding text should be entered into the textfield.

Could you please give some advice how to implement this properly? Also so that pickers/keyboards are dismissed after selection made/return pressed.

I found an answer to something similar here. ANd I managed to get the 2 picker-textfields working, but now I cannot make the keyboard appear for the rest. I guess it's because I'm overriding the textFieldShouldBegineEditing method. Any ideas to get round that? Can I somehow call the default method from inside textFieldShouldBeginEditing?? Note, if I uncomment my last lines of textFieldShouldBeginEditing I get a crash...

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
currentTextField = textField;

if (textField == self.pickerField1) {
    currentArray = self.array1;
    [pickerView reloadAllComponents];
    pickerView.hidden = NO;
    [self animatePickerViewIn];
    return NO;
}
if (textField == self.pickerField2){
    currentArray = self.array2;
    [pickerView reloadAllComponents];
    pickerView.hidden = NO;
    [self animatePickerViewIn];
    return NO;

}
//    else  {
//        [currentTextField becomeFirstResponder];
//        return NO;
//    }
- (void)pickerView:(UIPickerView *)pickerView 
  didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
[currentTextField setText:[currentArray objectAtIndex:row]];
[currentTextField resignFirstResponder];
pickerView.hidden = YES;
}

Thanks!

Community
  • 1
  • 1
fekioh
  • 894
  • 2
  • 9
  • 22

1 Answers1

1

Adding this for anyone that needs more information - here's how I got it work using user2135738's provided code:

MyViewController.h

#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController  <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *aTextField;
@property (weak, nonatomic) IBOutlet UITextField *bTextField;

@end

MyViewController.m

#import "MyViewController.h"

@interface MyViewController ()
@end

@implementation MyViewController {
   NSArray *aArray,
   *bArray,
   *currentArray;

   UITextField *currentTextField;
   UIPickerView *pickerView;
}

- (void)viewDidLoad {
   aArray = [NSArray arrayWithObjects: @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19, @20, nil];
   bArray = [NSArray arrayWithObjects:@"aString", @"bString", nil];

   [super viewDidLoad];

   pickerView = [[UIPickerView alloc] init];
   pickerView.dataSource = self;
   pickerView.delegate = self;
   self.aTextField.delegate = self;
   self.bTextField.delegate = self;

   self.aTextField.inputView = pickerView;
   self.bTextField.inputView = pickerView;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
   currentTextField = textField;

   if (textField == self.aTextField) {
      currentArray = aArray;
      [pickerView reloadAllComponents];
      pickerView.hidden = NO;
      return YES;
   } else if (textField == self.bTextField){
      currentArray = bArray;
      [pickerView reloadAllComponents];
      pickerView.hidden = NO;
      return YES;
   } else  {
      [currentTextField becomeFirstResponder];
      return NO;
   }

}

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
   return [currentArray count];
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
   return [NSString stringWithFormat:@"%@",[currentArray objectAtIndex:row]];
}

@end
Community
  • 1
  • 1
aashah7
  • 2,075
  • 1
  • 17
  • 24