0

I have a total of 5 text fields in a view controller. 1 of them opens the keyboard to fill in data. 2 of them open up the picker view to select the value. 1 opens up the Date Picker. And the last one segues to a Table View Controller.

The pickers and keyboard open up fine as I transition from one text field to another. If I click on the text field to segue to the table view, it opens as well. The problem occurs when I try to open the Table View if there is currently a picker or keyboard already active. The picker or keyboard from the previous selected text field shows in front of the table view. How do I hide the picker or keyboard before going to the table view?

Link to sample project http://www.filedropper.com/test_9

.h file

#import <UIKit/UIKit.h>

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

@property (weak, nonatomic) IBOutlet UITextField *textEntryName;
@property (weak, nonatomic) IBOutlet UITextField *textEntryStatus;
@property (weak, nonatomic) IBOutlet UITextField *textEntryLocation;
@property (weak, nonatomic) IBOutlet UITextField *textEntryGender;
@property (weak, nonatomic) IBOutlet UITextField *textDOB;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerAddEntryInfo;
@property (weak, nonatomic) IBOutlet UIDatePicker *datepickerAddEntryInfo;

@end

.m file

#import "AddAEntryViewController.h"

@interface AddAEntryViewController ()

@property(strong, nonatomic)NSArray *arrayEntryStatus;
@property(strong, nonatomic)NSArray *arrayEntryLocation;
@property(strong, nonatomic)NSArray *arrayEntryGender;

@end

@implementation AddAEntryViewController{

}

@synthesize textEntryName=_textEntryName;
@synthesize textEntryStatus = _textEntryStatus;
@synthesize textEntryLocation = _textEntryLocation;
@synthesize textEntryGender = _textEntryGender;
@synthesize textDOB =_textDOB;
@synthesize pickerAddEntryInfo = _pickerAddEntryInfo;
@synthesize datepickerAddEntryInfo = _datepickerAddEntryInfo;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //TOOLBAR SETUP

    CGRect cgRect =[[UIScreen mainScreen] bounds];
    CGSize cgSize = cgRect.size;

    UIToolbar* toolbar = [[UIToolbar alloc] init];
    toolbar.frame=CGRectMake(0, 0, cgSize.width, 35);
    toolbar.barStyle = UIBarStyleBlackTranslucent;

    //PICKER SETUP

    _pickerAddEntryInfo.hidden=YES;
    _arrayEntryStatus = [[NSArray alloc]initWithObjects:@"Single",
                     @"Married", nil];
    _arrayEntryGender = [[NSMutableArray alloc] initWithObjects:@"Female",@"Male",  nil];

    _textEntryStatus.inputView = _pickerAddEntryInfo;
    _textEntryGender.inputView = _pickerAddEntryInfo;

    _textEntryStatus.inputAccessoryView=toolbar;
    _textEntryGender.inputAccessoryView=toolbar;


    //DATEPICKER SETUP

    _datepickerAddEntryInfo.hidden=YES;

    [_datepickerAddEntryInfo setMaximumDate:maxDate];
    [_datepickerAddEntryInfo setMinimumDate:minDate];
    [_datepickerAddEntryInfo setDate:startDate];

    _textDOB.inputView = _datepickerAddEntryInfo;
    _textDOB.inputAccessoryView = toolbar;
}

 #pragma mark - Text Field Editing Begins

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    //Picker - these text fields open up the Picker View
    if (_textEntryStatus.editing == YES ||  _textEntryGender.editing == YES)
    {
        _pickerAddEntryInfo.hidden=NO;
    }

    //DatePicker - this text field opens up a Date Picker
    if (_textDOB.editing == YES)
    {
        _datepickerAddEntryInfo.hidden=NO;
    }

    //TableView for Entry Location - This text field will segue to a Table View Controller populated with data
    if (_textEntryLocation.editing == YES)
    {
        [textField resignFirstResponder];
    }
}


 #pragma mark - Text Field Editing Ends

-(void) textFieldDidEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
}
ObjCNoob
  • 1
  • 1
  • What about sending `resignFirstResponder` to that textField when performing segue to the tableView? – 0xNSHuman Aug 05 '14 at 19:09
  • I applied resignFirstResponder to that textField in order to hide the Keyboard from opening for editing. It does not hide any of the Pickers or the keyboard from the other textField. – ObjCNoob Aug 05 '14 at 20:24

1 Answers1

0

Use prepareForSegue: to perform any actions before segueing to another view controller.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"segueToMyTableView"]) {

        // send resignFirstResponder to all textFields
        [textEntryName resignFirstResponder];
        [textEntryStatus resignFirstResponder];
        [textEntryLocation resignFirstResponder];
        [textEntryGender resignFirstResponder];
        [textDOB resignFirstResponder];

        // and remove all pickers views from the superview
        [pickerAddEntryInfo removeFromSuperview];
        [datepickerAddEntryInfo removeFromSuperview];

    }
}

You can assign name to your segue in Storyboard like this (don't forget to select it on scheme)

enter image description here

Related documentation is here.

0xNSHuman
  • 638
  • 5
  • 8
  • What do you mean by "select it on scheme"? I tried your advice but still not working. I am able to pass a NSLog to tell me that the segue will occur but the resignFirstResponder action does not hide the pickers or keyboard from the other textFields. – ObjCNoob Aug 05 '14 at 20:21
  • @ObjCNoob I mean select segue itself (sorry if it sounds too obvious). Ok, are there not only one keyboard/picker need to be hidden during segue? If yes, then you need to call `resignFirstResponder` for other textFields too and `removeFromSuperview` for pickers. Check out my updated answer. – 0xNSHuman Aug 05 '14 at 20:36
  • I tried that as well. Still a no go. I have a simple sample project where I have recreated the issue but I am not sure how to upload it on here. – ObjCNoob Aug 05 '14 at 21:24
  • I have provided a link to the sample project in my initial post. – ObjCNoob Aug 05 '14 at 21:31
  • Well I have played a bit with your test project and the thing I've noticed is that you're using a modal presentation segue. Unfortunately, Apple made it impossible to send `resignFirstResponder` messages in this case. However, there is a similar problem with solution that might be effective for your case, but I was trying to implement it in your test project and nothing helped. Check out [this post](http://stackoverflow.com/questions/3019709/modal-dialog-does-not-dismiss-keyboard). – 0xNSHuman Aug 06 '14 at 07:35
  • And the last thing I can suggest you is to avoid using modal presentation in this particular case. Try to push that tableViewController instead. – 0xNSHuman Aug 06 '14 at 07:38
  • Thanks for trying Vlad. Unfortunately, the push segue did not work either so I inserted a placeholder view in front of the text boxes when the pickers and keyboards are active. This seems to hide the pickers and text boxes before proceeding to the table view. – ObjCNoob Aug 11 '14 at 15:01