(二)UITabBar and UINavigationController基础教程之UITextField键盘隐藏及防止键盘遮挡

来源:互联网 发布:中国进出口额数据 编辑:程序博客网 时间:2024/06/06 00:24
之前我们实现了页面切换
(一)UITabBar and UINavigationController基础教程之切换页面http://blog.csdn.net/zhangyankan/article/details/12833619


这次我们实现UITextField键盘隐藏及防止键盘遮挡

首先我们在.m文件中那个让viewcontroller实现UITextFieldDelegate

#import <UIKit/UIKit.h>#import "JBaseViewController.h"@interface JSecondViewController : JBaseViewController<UITextFieldDelegate>@end

接下来我们在.h文件中实现
#import "JSecondViewController.h"#import "StudentDataHelper.h"#import "Student.h"@interface JSecondViewController ()@property UITextField  *name;@property UITextField  *age;@property UITextField  *sex;@end@implementation JSecondViewController-(void)buildLayout{    //创建出需要的界面元素    NSArray *text = @[@"姓名",@"年龄",@"性别"];    for (int i=0; i<3; i++)    {        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(30, 100*i+70, 50, 30)];        lab.text = [text objectAtIndex:i];        lab.backgroundColor = [UIColor clearColor];        [self.view addSubview:lab];        [lab release];    }        _name = [[UITextField alloc]initWithFrame:CGRectMake(90, 70, 200, 30)];    _name.borderStyle = UITextBorderStyleRoundedRect;    [self.view addSubview:_name];    [_name release];        _age = [[UITextField alloc]initWithFrame:CGRectMake(90, 170, 200, 30)];    _age.borderStyle = UITextBorderStyleRoundedRect;    [self.view addSubview:_age];    [_age release];        _sex = [[UITextField alloc]initWithFrame:CGRectMake(90, 270, 200, 30)];    _sex.borderStyle = UITextBorderStyleRoundedRect;    [self.view addSubview:_sex];    [_sex release];        UIButton * btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];    btn.frame=CGRectMake(90, 340, 150, 40);    btn.backgroundColor=[UIColor clearColor];    [btn setTitle:@"保存" forState:UIControlStateNormal];    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    [btn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];    [btn release];}-(void)save{    //这里就先不介绍怎么具体存入数据库了    //NSLog(@"==1==");    Student * student=[[Student alloc]init];    student.name=_name.text;    student.sex=_sex.text;    student.age=_age.text;    StudentDataHelper * stu=[StudentDataHelper sharedHelper];    //NSLog(@"%@",student.name);    [stu addStudent:student];    [student release];}//隐藏输入键盘 (点击return)-  (BOOL)textFieldShouldReturn:(id)sender{[sender  resignFirstResponder];    return  YES;}//隐藏输入键盘(文本框 失去焦点键盘隐藏)-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    [_name resignFirstResponder];    [_age resignFirstResponder];    [_sex resignFirstResponder];}-(void)textFieldDidBeginEditing:(id)sender{    if (sender==_sex) {        //CGRectOffset,让一个矩阵偏移一定的量        CGRect target = CGRectOffset(self.view.frame, 0, -80);                [UIView animateWithDuration:0.25 animations:^{            self.view.frame = target;}];    }}- (void)textFieldDidEndEditing:(id)sender{    if (sender==_sex) {        CGRect target = CGRectOffset(self.view.frame, 0, 80);                [UIView animateWithDuration:0.25 animations:^{            self.view.frame = target;}];    }}- (void)viewDidLoad{    [super viewDidLoad];    self.view.backgroundColor=[UIColor grayColor];    [self buildLayout];    //隐藏navigationController    [self.navigationController setNavigationBarHidden:YES animated:YES];    //把它们3个属性的代理设成自身    _name.delegate=self;    _age.delegate=self;    _sex.delegate=self;}@end

这样就可以基本实现了