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