iphone-使用TextField及关闭键盘(useing TextField for inputs、

来源:未知 责任编辑:智问网络 发表时间:2013-10-07 00:58 点击:
 创建项目,名字为KeyBoard,我用的是xcode4.2!
在MainStoryboard.storyboard文件里拖四个label和四个TextField,如下界面:

填满内容:
点击完成Done键盘会消失!!
首先我先说说四个TextField的属性分别对应如下:
name:

age:keyboard改成Numbers and Punctuation
password:把Secure属性勾上 www.2cto.com
email:keyBoard发成E-mail Address
接下来是在KeyboardViewController.h文件定义如下:
 

<span style="font-size:16px;">- (IBAction)doneEdit:(id)sender;</span> 
<span style="font-size:16px;">- (IBAction)doneEdit:(id)sender;</span>在KeyboardViewController.m文件实现如下:


[html]  <span style="font-size:16px;">- (IBAction)doneEdit:(id)sender { 
    [sender resignFirstResponder]; 
}</span> 
<span style="font-size:16px;">- (IBAction)doneEdit:(id)sender {
    [sender resignFirstResponder];
}</span>
把四个TextFiled的Did​ End​ on​ Exit做连接出口IBAction,连到doneEdit方法上!这个大家都知道怎么连哈!在此不给出图例了!
[sender resignFirstResponder],是要求文本字段第一响应者的地位辞职,这就意味着不再需要键盘与文本字段的交互了,使其隐藏!

这样,效果就达到了,还有人会想:“我不想按Done键使其隐藏,我想使它按下后面的背景就把键盘隐藏了。”,不要急,接下来就说这种情况!!!!

还在原来的项目中进行,在视图中添加一个按钮,一个很大的按钮,能把正个视图盖住,把Type属性改成Custom,并把按钮拉到所有控件的上面,也就是第一位置如下图,这样做是为了不让按钮挡住所有按钮!

 \

 

在KeyboardViewController.h文件中添加代码如下:


[cpp] @interface KeyboardViewController : UIViewController{ 
    UITextField *email; 
    UITextField *password; 
    UITextField *age; 
    UITextField *name; 

@property (retain, nonatomic) IBOutlet UITextField *email; 
@property (retain, nonatomic) IBOutlet UITextField *password; 
@property (retain, nonatomic) IBOutlet UITextField *age; 
@property (retain, nonatomic) IBOutlet UITextField *name; 
- (IBAction)buttonEdit:(id)sender; 
- (IBAction)doneEdit:(id)sender; 
@end 
@interface KeyboardViewController : UIViewController{
    UITextField *email;
    UITextField *password;
    UITextField *age;
    UITextField *name;
}
@property (retain, nonatomic) IBOutlet UITextField *email;
@property (retain, nonatomic) IBOutlet UITextField *password;
@property (retain, nonatomic) IBOutlet UITextField *age;
@property (retain, nonatomic) IBOutlet UITextField *name;
- (IBAction)buttonEdit:(id)sender;
- (IBAction)doneEdit:(id)sender;
@end
并把button按钮Touch Up Inside事件连接到buttonEdit;

在KeyboardViewController.m文件实现:


[cpp]  @synthesize email; 
@synthesize password; 
@synthesize age; 
@synthesize name; 
- (IBAction)buttonEdit:(id)sender { 
    [email resignFirstResponder]; 
    [password resignFirstResponder]; 
    [age resignFirstResponder]; 
    [name resignFirstResponder]; 

@synthesize email;
@synthesize password;
@synthesize age;
@synthesize name;
- (IBAction)buttonEdit:(id)sender {
    [email resignFirstResponder];
    [password resignFirstResponder];
    [age resignFirstResponder];
    [name resignFirstResponder];
}

这样就实现了点击背影就关闭键盘了。

还有人会想:“我想一打开应用就打开键盘并且光标在name框内”。

那么就在viewDidLoad  里写入代码:


[cpp]  - (void)viewDidLoad 

    [name becomeFirstResponder]; 
    [super viewDidLoad]; 

- (void)viewDidLoad
{
    [name becomeFirstResponder];
    [super viewDidLoad];
}
嗯!


学习过程 中不怕麻烦,希望跟大家一块努力学习!有什么不好的地方,请多指出!!!

 

摘自 任海丽(3G/移动开发)

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Lexical or Preprocessor Issue 'xxx.h
    • ios学习笔记(二)xcode 4.3.2下实现基本交互
    • ios版本的helloworld
    • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
    • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
    • IOS类似iphone通讯录TableView的完整demo【附源码】
    • UITableView一些方法
    • [iPhone中级]iPhone团购信息客户端的开发 (二)
    • 如何为Iphone应用创建启动界面
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1