iPhone开发监听键盘状态、输入法状态

本文介绍如何在iOS应用中实现监听键盘状态变化和输入法切换,包括代码实现与实例解析。
实现的功能:1)演示监听键盘状态(可解决键盘挡住输入法等问题)2)监听输入法状态

关键词:键盘状态 输入法状态 监听


1、新建一SingleView Application,命名为:KeyBoard&InputMethod,工程结果如下:
[img]
[img]http://dl.iteye.com/upload/attachment/0078/7569/81237d8b-3f1e-30d1-96f7-0da46df0b585.png[/img]
[/img]


2、修改ViewController.xib如下:
[img]
[img]http://dl.iteye.com/upload/attachment/0078/7571/e90165f1-922e-3667-a8e1-e1f013183b76.png[/img]
[/img]


3、ViewController.h不作修改,ViewController.m修改后如下:
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//监听键盘状态
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
//监听输入法状态
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeInputMode:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];
}

#pragma mark Notification
//keyBoard已经展示出来
- (void)keyboardDidShow:(NSNotification *)notification
{
NSValue* aValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGRect keyboardFrame = [self.view convertRect:keyboardRect fromView:[[UIApplication sharedApplication] keyWindow]];
CGFloat keyboardHeight = keyboardFrame.size.height;
NSLog(@"##keboardHeight=%.2f",keyboardHeight);
}

//输入法发生切换
-(void)changeInputMode:(NSNotification *)notification{
NSString *inputMethod = [[UITextInputMode currentInputMode] primaryLanguage];
NSLog(@"inputMethod=%@",inputMethod);
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值