您的位置:首页 > 移动开发 > IOS开发

iOS 之给苹果自带的纯数字键盘添加完成事件

2013-09-10 17:46 295 查看
.h文件中添加一个按钮的属性

{
UIButton *doneInKeyboardButton;
}

在.m文件中添加以下代码就可实现纯数字代码添加完成事件

- (void)viewDidLoad
{
[super
viewDidLoad];
//注册通知
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(handleKeyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(handleKeyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}

#pragma mark 键盘
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
if (doneInKeyboardButton.superview)
{
[doneInKeyboardButton
removeFromSuperview];
}
}
- (void)handleKeyboardDidShow:(NSNotification *)notification
{
if (doneInKeyboardButton ==
nil)
{
doneInKeyboardButton = [UIButton
buttonWithType:UIButtonTypeCustom];

CGFloat screenHeight = [[UIScreen
mainScreen] bounds].size.height;
if(screenHeight==568.0f){//爱疯5
doneInKeyboardButton.frame =
CGRectMake(0, 568 -
53, 106, 53);
}else{//3.5寸
doneInKeyboardButton.frame =
CGRectMake(0, 480 -
53, 106, 53);
}

doneInKeyboardButton.adjustsImageWhenHighlighted =
NO;
//图片直接抠腾讯财付通里面的= =!
[doneInKeyboardButton
setImage:[UIImage
imageNamed:@"btn_done_up@2x.png"]
forState:UIControlStateNormal];
[doneInKeyboardButton
setImage:[UIImage
imageNamed:@"btn_done_down@2x.png"]
forState:UIControlStateHighlighted];
[doneInKeyboardButton
addTarget:self
action:@selector(finishAction)
forControlEvents:UIControlEventTouchUpInside];
}


UIWindow* tempWindow = [[[UIApplication
sharedApplication] windows]
objectAtIndex:1];

if (doneInKeyboardButton.superview ==
nil)
{
[tempWindow addSubview:doneInKeyboardButton];
// 注意这里直接加到window上
}

}
//点击完成后的时间
-(void)finishAction
{
NSLog(@"aa");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: