从文本字段打开新的TableView控件时隐藏选取器和键盘

时间:2014-08-05 18:51:58

标签: ios objective-c uitableview uipickerview

视图控制器中总共有5个文本字段。其中1个打开键盘以填充数据。其中2个打开选择器视图以选择值。 1打开日期选择器。最后一个是一个表视图控制器。

当我从一个文本字段转换到另一个文本字段时,拾取器和键盘打开正常。如果我单击文本字段以转到表视图,它也会打开。当我尝试打开表视图时,如果当前有一个选择器或键盘已经激活,则会出现此问题。上一个选定文本字段中的选取器或键盘显示在表视图的前面。如何在进入表格视图之前隐藏选择器或键盘?

链接到示例项目http://www.filedropper.com/test_9

.h文件

#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文件

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

1 个答案:

答案 0 :(得分:0)

使用prepareForSegue:在转换到另一个视图控制器之前执行任何操作。

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

    }
}

你可以像这样在故事板中为你的segue分配名字(不要忘记在方案上选择它)

enter image description here

相关文档为here