(二)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
这样就可以基本实现了
- (二)UITabBar and UINavigationController基础教程之UITextField键盘隐藏及防止键盘遮挡
- (一)UITabBar and UINavigationController基础教程之切换页面
- UITextField防止键盘遮挡以及添加手势操作消失键盘
- uitextfield键盘遮挡问题
- 键盘遮挡问题(UITextField)
- UITableView上的UITextField防止被键盘遮挡
- TextField防止键盘遮挡
- 防止键盘遮挡
- (三)UITabBar and UINavigationController基础教程之UITableView的插入删除移动
- uitextfield 键盘遮挡时上移
- UITextField被键盘遮挡问题
- 解决键盘遮挡UITextField问题
- UITextField被弹出键盘遮挡
- ios键盘遮挡UITextField问题
- 如何处理键盘遮挡UITextField
- iOS 隐藏键盘,输入法,防止遮挡输入框
- ios--解决键盘遮挡UITextField控件的问题(方式二)
- iOS开发学习笔记——文本框(UITextField)及键盘遮挡处理和回收
- httpclient向HTTPS发送数据建立SSL连接时的异常
- 【C++】如何生成数字连续的文件名
- LibLinear
- FileZilla Server的安装和设置
- 艺术与生活
- (二)UITabBar and UINavigationController基础教程之UITextField键盘隐藏及防止键盘遮挡
- 我希望在软件开发生涯初期就知道的 4 件事
- HDU1150 二分匹配 最少点覆盖=最大匹配数
- ACM题目,输出A/B。如果整除则输出整数,如果不能整除则保留两位小数,除式不成立则输出no。每组输出占一行。
- CSS 图片拼合
- 【ArcGIS API for Flex】GraphicLayer与Graphic
- wireshark过滤规则
- Spring MVC中Controller如何区分一个Form中包含的多个Submit按钮
- 网络子系统64_设备ip地址配置