Iphone开发(四)文本框,文本视图,和软键盘的隐藏
2012-05-15 16:00
351 查看
holydancer原创,如需转载,请在显要位置注明:
转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/7396207
今天介绍几个基本控件和软键盘的操作,在iphone应用中用到一些文本编缉时,软键盘不会像android那样,在输入完成后点返回键自动隐藏,需要你写代码实现,所以键盘的隐藏也算是iphone开发的一个基础了。iphone开发中的文本框UITextField类似于android中的EditText,可以用于输入内容,而文本视图UITextView继承自UIScrollView类,可以进行滚动,字体编辑等,我们可以这样理解,UITextFiled可以用于短信界面的联系人输入,UITextView可以用于短信界面的短信内容输入。好吧,既然这样说了,今天我们就以一个模拟的短信发送界面来介绍一下文本框和文本视图,以及如何处理键盘的隐藏。
首先新建一个项目,然后打开xib文件,进行一下界面的构建:
拖好后我们先不进行代码的工作,先来看一下UITextField的属性栏,如下图:
text表示初始化时自动出现在框中的文字,placeholder表示隐藏提示,如图所示,类似于android中的hint,还有一些背景设置,对齐格式,边框样式,字体大小设置等,往下还有透明度等,截图中没有截上,总之就是一些常用的属性,注意Keyboard一栏可以设置键盘的格式,图中所示是只能输入数字的键盘。在UITextView中也与此差不多,但是多了这么一个属性栏 :
这个属性栏是设置UITextView的滚动属性的,一般不用做太大的修改。好了,下面我们来进行view和controller的连接,因为我们不打算对两个label和button进行操作,所以我们就不对他们进行输出口的声明了,仅仅声明UITextField和UITextView的输出口。如图所示,这样我们就可以对其在代码中进行操作。
viewController.h:
[plain]
view plaincopy
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *myTextField;
@property (retain, nonatomic) IBOutlet UITextView *myTextView;
@end
viewController.m:
[html]
view plaincopy
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myTextField;
@synthesize myTextView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setMyTextField:nil];
[self setMyTextView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[myTextField release];
[myTextView release];
[super dealloc];
}
@end
这时运行模拟器我们会发现在点击文本框时会自动调出软键盘,但是输入完成后,该键盘不会自动关闭,这时我们需要点击xib文件中的第三个图标View,这是当前程序的主视图,然后如下图所示,将class改为UIControl,这时,整个大的视图就可以响应方法了。
通过更改UIView为UIControl,我们可以在触摸屏幕上非活动控件时都会产生Touch Down事件,我们需要在类中定义一个IBAction方法用以对应这个事件,在产生Touch Down事件时,会调用这个方法,然后该方法的实现中失去两个文本编辑框的响应者身份,代码为:
[ myTextField resignFirstResponder];
[myTextView resignFirstResponder];
这样在输入完成后,触摸屏幕上的其他地方,不管两个编辑框谁是当前的第一响应者,都会失去焦点,然后软键盘会自动关闭。下在我们先进行IBAction方法的实现,我们起名叫closeType方法:
viewController.h:
[plain]
view plaincopy
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *myTextField;
@property (retain, nonatomic) IBOutlet UITextView *myTextView;
-(IBAction)closeType:(id)sender;
@end
viewController.m:
[plain]
view plaincopy
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myTextField;
@synthesize myTextView;
-(IBAction)closeType:(id)sender
{
[myTextView resignFirstResponder];
[myTextField resignFirstResponder];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setMyTextField:nil];
[self setMyTextView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[myTextField release];
[myTextView release];
[super dealloc];
}
@end
然后viewController中有了一个可以和xib文件进行对接的方法,在xib文件中找到touch down方法,拖拽到左边的File's Owner,点击出现的closeType方法,这样在点击屏幕中空闲地方时,会触动touch down行为,然后呢,会调用viewController中的closeType方法,将当前的第一响应者身份取消,这样软键盘就会消失。
转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/7396207
今天介绍几个基本控件和软键盘的操作,在iphone应用中用到一些文本编缉时,软键盘不会像android那样,在输入完成后点返回键自动隐藏,需要你写代码实现,所以键盘的隐藏也算是iphone开发的一个基础了。iphone开发中的文本框UITextField类似于android中的EditText,可以用于输入内容,而文本视图UITextView继承自UIScrollView类,可以进行滚动,字体编辑等,我们可以这样理解,UITextFiled可以用于短信界面的联系人输入,UITextView可以用于短信界面的短信内容输入。好吧,既然这样说了,今天我们就以一个模拟的短信发送界面来介绍一下文本框和文本视图,以及如何处理键盘的隐藏。
首先新建一个项目,然后打开xib文件,进行一下界面的构建:
拖好后我们先不进行代码的工作,先来看一下UITextField的属性栏,如下图:
text表示初始化时自动出现在框中的文字,placeholder表示隐藏提示,如图所示,类似于android中的hint,还有一些背景设置,对齐格式,边框样式,字体大小设置等,往下还有透明度等,截图中没有截上,总之就是一些常用的属性,注意Keyboard一栏可以设置键盘的格式,图中所示是只能输入数字的键盘。在UITextView中也与此差不多,但是多了这么一个属性栏 :
这个属性栏是设置UITextView的滚动属性的,一般不用做太大的修改。好了,下面我们来进行view和controller的连接,因为我们不打算对两个label和button进行操作,所以我们就不对他们进行输出口的声明了,仅仅声明UITextField和UITextView的输出口。如图所示,这样我们就可以对其在代码中进行操作。
viewController.h:
[plain]
view plaincopy
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *myTextField;
@property (retain, nonatomic) IBOutlet UITextView *myTextView;
@end
viewController.m:
[html]
view plaincopy
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myTextField;
@synthesize myTextView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setMyTextField:nil];
[self setMyTextView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[myTextField release];
[myTextView release];
[super dealloc];
}
@end
这时运行模拟器我们会发现在点击文本框时会自动调出软键盘,但是输入完成后,该键盘不会自动关闭,这时我们需要点击xib文件中的第三个图标View,这是当前程序的主视图,然后如下图所示,将class改为UIControl,这时,整个大的视图就可以响应方法了。
通过更改UIView为UIControl,我们可以在触摸屏幕上非活动控件时都会产生Touch Down事件,我们需要在类中定义一个IBAction方法用以对应这个事件,在产生Touch Down事件时,会调用这个方法,然后该方法的实现中失去两个文本编辑框的响应者身份,代码为:
[ myTextField resignFirstResponder];
[myTextView resignFirstResponder];
这样在输入完成后,触摸屏幕上的其他地方,不管两个编辑框谁是当前的第一响应者,都会失去焦点,然后软键盘会自动关闭。下在我们先进行IBAction方法的实现,我们起名叫closeType方法:
viewController.h:
[plain]
view plaincopy
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *myTextField;
@property (retain, nonatomic) IBOutlet UITextView *myTextView;
-(IBAction)closeType:(id)sender;
@end
viewController.m:
[plain]
view plaincopy
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myTextField;
@synthesize myTextView;
-(IBAction)closeType:(id)sender
{
[myTextView resignFirstResponder];
[myTextField resignFirstResponder];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setMyTextField:nil];
[self setMyTextView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[myTextField release];
[myTextView release];
[super dealloc];
}
@end
然后viewController中有了一个可以和xib文件进行对接的方法,在xib文件中找到touch down方法,拖拽到左边的File's Owner,点击出现的closeType方法,这样在点击屏幕中空闲地方时,会触动touch down行为,然后呢,会调用viewController中的closeType方法,将当前的第一响应者身份取消,这样软键盘就会消失。
相关文章推荐
- Iphone开发(四)文本框,文本视图,和软键盘的隐藏
- Iphone开发 文本框,文本视图,和软键盘的隐藏
- Iphone开发(四)文本框,文本视图,和软键盘的隐藏
- Iphone开发(四)文本框,文本视图,和软键盘的隐藏
- iphone开发笔记(-) 退回输入键盘:自定义颜色:隐藏状态栏:
- IPhone开发 IPad使用UIModalPresentationFormSheet时隐藏键盘
- iOS开发----键盘弹出和隐藏时移动视图,防止被键盘挡住
- Objective-C ,ios,iphone开发基础:在UITextField输入完以后,隐藏键盘,
- iPhone开发笔记 退回输入键盘:自定义颜色:隐藏状态栏:
- [置顶] Objective-C ,ios,iphone开发基础:在UITextField输入完以后,隐藏键盘,
- ios开发: 当点击其他地方时, 隐藏文本框弹出的键盘
- iPhone界面上文本框UITextField的软键盘隐藏(取消第一响应)和移动键盘下面的内容
- 【iOS开发-16】UITextField协议的用法,键盘的隐藏,以及视图随着键盘的出现隐藏而上下调整位置
- iPhone开发笔记 退回输入键盘:自定义颜色:隐藏状态栏:
- [IPhone开发]如何在输入完成时自动隐藏键盘
- 【iOS开发-15】UITextField常用属性归纳:文本框样式、文字样式、键盘样式、左右视图样式、清除按钮设置等
- iPhone界面上文本输入时隐藏键盘
- iOS 开发 UITextField常用属性:文本框样式、文字样式、键盘样式、左右视图样式、清除按钮设置等
- iOS开发 键盘处理 (隐藏,自动转到下个文本框,轻触背景隐藏)
- 单击视图空白处隐藏IPhone键盘