iOS 自定义键盘
2013-10-18 16:51
253 查看
//
// KeyBoardViewController.m
// KeyBoard
//
// Created by CaoYang on 13-10-17.
// Copyright (c) 2013年 Young. All rights reserved.
//
#import
"KeyBoardViewController.h"
@interface KeyBoardViewController ()<UITextFieldDelegate>
{
UIButton *doneInKeyboardButton;
NSString * strNumOrOther;
}
@end
@implementation KeyBoardViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
//键盘出现之后
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(handleKeyboardDidShow:)
name:UIKeyboardWillShowNotification
object:nil];
//键盘即将消失
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(handleKeyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
UITextField * textField = [[UITextField
alloc]init];
textField.frame =
CGRectMake(10,
64, 190,
36);
[textField
setText:@"点击编辑分数0~100"];
[textField
setTag:1001];
[textField
setClearsOnBeginEditing:YES];
[textField
setBackgroundColor:[UIColor
orangeColor]];
[textField
setKeyboardType:UIKeyboardTypeNumberPad];
[textField
setDelegate:self];
[self.view
addSubview:textField];
UITextField * textFieldA = [[UITextField
alloc]init];
textFieldA.frame =
CGRectMake(10,
150, 300,
160);
[textFieldA
setText:@"点击编辑每日总结"];
[textFieldA
setClearsOnBeginEditing:YES];
[textFieldA
setBackgroundColor:[UIColor
orangeColor]];
[textFieldA
setDelegate:self];
[textFieldA
setTag:400];
[self.view
addSubview:textFieldA];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
UITextField * tex = (UITextField*)[self.view
viewWithTag:400];
UITextField * tex1 = (UITextField*)[self.view
viewWithTag:1001];
if(tex==textField)
{
doneInKeyboardButton.hidden =
YES;
strNumOrOther = @"aa";
}
else if(textField==tex1)
{
doneInKeyboardButton.hidden =
NO;
strNumOrOther = @"bb";
}
return YES;
}
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
if (doneInKeyboardButton.superview)
{
doneInKeyboardButton.hidden =
YES;
}
}
- (void)handleKeyboardDidShow:(NSNotification *)notification
{
UIWindow* tempWindow = [[[UIApplication
sharedApplication] windows]
objectAtIndex:1];
doneInKeyboardButton = [UIButton
buttonWithType:UIButtonTypeCustom];
doneInKeyboardButton.frame =
CGRectMake(0,
480 - 53, 106,
53);
[doneInKeyboardButton
setTitle:@"AAA"
forState:UIControlStateNormal];
[doneInKeyboardButton
addTarget:self
action:@selector(finishAction)
forControlEvents:UIControlEventTouchUpInside];
[tempWindow
addSubview:doneInKeyboardButton];
// 注意这里直接加到window上
if([strNumOrOther
isEqualToString:@"aa"])
{
doneInKeyboardButton.hidden =
YES;
}
else
{
doneInKeyboardButton.hidden =
NO;
}
}
-(void)finishAction
{
UITextField * te = (UITextField*)[self.view
viewWithTag:1001];
[te
resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField
resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
// KeyBoardViewController.m
// KeyBoard
//
// Created by CaoYang on 13-10-17.
// Copyright (c) 2013年 Young. All rights reserved.
//
#import
"KeyBoardViewController.h"
@interface KeyBoardViewController ()<UITextFieldDelegate>
{
UIButton *doneInKeyboardButton;
NSString * strNumOrOther;
}
@end
@implementation KeyBoardViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
//键盘出现之后
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(handleKeyboardDidShow:)
name:UIKeyboardWillShowNotification
object:nil];
//键盘即将消失
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(handleKeyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
UITextField * textField = [[UITextField
alloc]init];
textField.frame =
CGRectMake(10,
64, 190,
36);
[textField
setText:@"点击编辑分数0~100"];
[textField
setTag:1001];
[textField
setClearsOnBeginEditing:YES];
[textField
setBackgroundColor:[UIColor
orangeColor]];
[textField
setKeyboardType:UIKeyboardTypeNumberPad];
[textField
setDelegate:self];
[self.view
addSubview:textField];
UITextField * textFieldA = [[UITextField
alloc]init];
textFieldA.frame =
CGRectMake(10,
150, 300,
160);
[textFieldA
setText:@"点击编辑每日总结"];
[textFieldA
setClearsOnBeginEditing:YES];
[textFieldA
setBackgroundColor:[UIColor
orangeColor]];
[textFieldA
setDelegate:self];
[textFieldA
setTag:400];
[self.view
addSubview:textFieldA];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
UITextField * tex = (UITextField*)[self.view
viewWithTag:400];
UITextField * tex1 = (UITextField*)[self.view
viewWithTag:1001];
if(tex==textField)
{
doneInKeyboardButton.hidden =
YES;
strNumOrOther = @"aa";
}
else if(textField==tex1)
{
doneInKeyboardButton.hidden =
NO;
strNumOrOther = @"bb";
}
return YES;
}
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
if (doneInKeyboardButton.superview)
{
doneInKeyboardButton.hidden =
YES;
}
}
- (void)handleKeyboardDidShow:(NSNotification *)notification
{
UIWindow* tempWindow = [[[UIApplication
sharedApplication] windows]
objectAtIndex:1];
doneInKeyboardButton = [UIButton
buttonWithType:UIButtonTypeCustom];
doneInKeyboardButton.frame =
CGRectMake(0,
480 - 53, 106,
53);
[doneInKeyboardButton
setTitle:@"AAA"
forState:UIControlStateNormal];
[doneInKeyboardButton
addTarget:self
action:@selector(finishAction)
forControlEvents:UIControlEventTouchUpInside];
[tempWindow
addSubview:doneInKeyboardButton];
// 注意这里直接加到window上
if([strNumOrOther
isEqualToString:@"aa"])
{
doneInKeyboardButton.hidden =
YES;
}
else
{
doneInKeyboardButton.hidden =
NO;
}
}
-(void)finishAction
{
UITextField * te = (UITextField*)[self.view
viewWithTag:1001];
[te
resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField
resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- ALAssetsLibrary-代码操作iOS相册资源
- 吐槽iOS国际化:关于NSLocalizedString的使用
- iOS PSTCollectionView使用注意点
- 请确认在您的电脑的BIOS中已启用VT-x/AMD-V支持
- 请确认在您的电脑的BIOS中已启用VT-x/AMD-V支持
- ios sandbox
- 小谈AFNetworking的项目使用
- iOS应用程序——介绍挂起重新运行场景
- xcode:关于Other Linker Flags
- IOS开发 ios7适配
- 问题:Undefined symbols for architecture armv7
- 苹果开发者账号申请
- 第一章 NetBIOS
- nagios安装snmp插件
- iOS唯一标示符引导-详解
- github-IOS备忘
- iOS 6.0 强制横屏
- “iOS 推送通知”详解:从创建到设置到运行
- IOS中TextField点击取消键盘
- xcode5使用ios6sdk