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

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: