在创建一个自定义键盘的时候遇到的错误
//双重for循环,对于Button上的数字用二维数组
// NSArray * butArr[4][3] = {@[@"1",@"2",@"3"],@[@"4",@"5",@"6"],@[@"7",@"8",@"9"],@[@"✔️",@"0",@"?"]};错误
// NSArray * butArr[4][3] = @[@[@"1",@"2",@"3"],@[@"4",@"5",@"6"],@[@"7",@"8",@"9"],@[@"✔️",@"0",@"?"]];错误
正确的写法是:
NSArray * butArr = @[@[@"1",@"2",@"3"],@[@"4",@"5",@"6"],@[@"7",@"8",@"9"],@[@"✔️",@"0",@"?"]];
在数组定义的时候,只要写上数组名就行
自定义键盘、设置输入框的辅助视图
_tf1.inputView = self.KeyView;//输入时候的键盘为自定义键盘
// //设置输入框的辅助视图 也可在创建 KeyView 的时候创建
_tf1.inputAccessoryView = self.accessaryView;
//下面两种方法都可以获得 Button 按钮上的文字
// NSString * str1 = [NSString stringWithFormat:@"%@",_but3.titleLabel.text];
// NSString * str2 = [_but3 titleForState:UIControlStateNormal];
注意移除计时器的时候,要在dealloc 里面将计时器置为nil,然后再让其作废


#define RandomColor [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:arc4random()%256/255.0] #import "AppDelegate.h" @interface AppDelegate () @property(nonatomic,retain)NSTimer * timer; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height-100)]; view.tag = 101; view.backgroundColor = [UIColor redColor]; [self.window addSubview:view]; CGFloat width = self.window.frame.size.width; CGFloat height = self.window.frame.size.height; UIButton * starButton = [UIButton buttonWithType:UIButtonTypeSystem]; starButton.frame = CGRectMake(width -300, height - 80, 80, 30); [starButton addTarget:self action:@selector(handleStar:) forControlEvents:UIControlEventTouchUpInside]; [starButton setTitle:@"star" forState:UIControlStateNormal]; [self.window addSubview:starButton]; UIButton * endButton = [UIButton buttonWithType:UIButtonTypeSystem]; endButton.frame = CGRectMake(width - 200, height - 80, 80, 30); [endButton addTarget:self action:@selector(handleEnd:) forControlEvents:UIControlEventTouchUpInside]; [endButton setTitle:@"end" forState:UIControlStateNormal]; [self.window addSubview:endButton]; return YES; } -(void)handleStar:(UIButton *)sender{//开启计时器 self.timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(changColor:) userInfo:nil repeats:YES]; } -(void)handleEnd:(UIButton *)sender{ [self.timer invalidate];//关闭计时器 } -(void)changColor:(NSTimer *)mytimer{//计时器触发的事件实现 UIView * uiv = [self.window viewWithTag:101]; uiv.backgroundColor = RandomColor; }
代码


#import@interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; -(void)dealloc; @end


#import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate -(void)dealloc{ // [self releas]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; RootViewController * RootVC = [[RootViewController alloc]init]; self.window.rootViewController = RootVC; [RootVC release]; return YES; }


#import@interface RootViewController : UIViewController @property(nonatomic,retain)UILabel * lab1,*lab2,*lab3,*lab4; @property(nonatomic,retain)UITextField * tf1,* tf2,*tf3; @property(nonatomic,retain)UIButton * but1,* but2,*but3; @property(nonatomic,assign)int a,b,c,d; @end


// // RootViewController.m #define RandomColor [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0] #import "RootViewController.h" @interface RootViewController ()@property(nonatomic,retain)UIView * KeyView; @property(nonatomic,retain)UILabel * accessaryView;//键盘辅助视图 @property(nonatomic,retain)UITextField * tf;//来存储当前操作的输入框 @end @implementation RootViewController -(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self; } - (void)viewDidLoad { [super viewDidLoad]; //创建自定义键盘视图 [self setUpCoustomKeyBoardView];//要写在 setUpTextField 之前,因为下文要使用到 inputIvew 方法 [self setUpLabel]; [self setUpTextField]; [self setUpButton]; } //布局页面 -(void)setUpLabel{ self.lab1 = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 60, 30)]; _lab1.text = @"用户名"; [self.view addSubview:_lab1]; [_lab1 release]; self.lab2 =[[UILabel alloc]initWithFrame:CGRectMake(50, 110, 60, 30)]; _lab2.text =@"密码"; [self.view addSubview:_lab2]; [_lab2 release]; self.lab3 =[[UILabel alloc]initWithFrame:CGRectMake(50, 170, 60, 30)]; _lab3.text =@"验证码"; [self.view addSubview:_lab3]; [_lab3 release]; //背景图片 self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"v1"]]; // self.lab4 =[[UILabel alloc]initWithFrame:CGRectMake(300, 170, 60, 30)]; // _lab4.text =@"1231"; // _lab4.textAlignment = NSTextAlignmentCenter; // _lab4.backgroundColor = [UIColor grayColor]; // [self.view addSubview:_lab4]; // [_lab4 release]; } -(void)setUpTextField{ //用户名输入框 self.tf1 = [[UITextField alloc]initWithFrame:CGRectMake(110, 50, 180, 30)]; _tf1.placeholder = @"请输入用户名"; _tf1.delegate = self; _tf1.inputView = self.KeyView;//输入时候的键盘为自定义键盘 // //设置输入框的辅助视图 也可在创建 KeyView 的时候创建 _tf1.inputAccessoryView = self.accessaryView; _tf1.layer.borderWidth = 1; _tf1.layer.borderColor = [UIColor blackColor].CGColor; _tf1.layer.cornerRadius = 8; [self.view addSubview:_tf1]; [_tf1 release]; //密码输入框 self.tf2 = [[UITextField alloc]initWithFrame:CGRectMake(110, 110, 180, 30)]; _tf2.placeholder = @"请输入密码"; _tf2.delegate = self; _tf2.inputView = self.KeyView; _tf2.layer.borderWidth = 1; _tf2.secureTextEntry = YES; _tf2.layer.borderColor = [UIColor blackColor].CGColor; _tf2.layer.cornerRadius = 8; [self.view addSubview:_tf2]; [_tf2 release]; //验证码输入框 self.tf3 = [[UITextField alloc]initWithFrame:CGRectMake(110, 170, 120, 30)]; _tf3.text = @""; _tf3.delegate = self; _tf3.inputView = self.KeyView; _tf3.layer.borderWidth = 1; _tf3.layer.borderColor = [UIColor blackColor].CGColor; _tf3.layer.cornerRadius = 8; _tf3.placeholder = @"输入验证码"; [self.view addSubview:_tf3]; [_tf3 release]; } -(void)setUpButton{ //登陆按钮 self.but1 = [UIButton buttonWithType:UIButtonTypeSystem]; _but1.frame = CGRectMake(150, 220, 50, 30); _but1.layer.borderWidth = 1; _but1.layer.cornerRadius = 8; [_but1 setTitle:@"登陆" forState:UIControlStateNormal]; [_but1 addTarget:self action:@selector(butAction1:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_but1]; //取消登陆 self.but2 = [UIButton buttonWithType:UIButtonTypeSystem]; _but2.frame = CGRectMake(220, 220, 50, 30); _but1.layer.borderWidth = 1; _but1.layer.cornerRadius = 8; [_but2 setTitle:@"取消" forState:UIControlStateNormal]; [_but2 addTarget:self action:@selector(butAction2:) forControlEvents:UIControlEventTouchUpInside]; _but2.layer.borderWidth = 1; _but2.layer.cornerRadius = 8; [self.view addSubview:_but2]; //刷新验证码 self.but3 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; _but3.frame = CGRectMake(240, 170, 60, 30); _but3.layer.backgroundColor = RandomColor.CGColor; [_but3 addTarget:self action:@selector(butAction3:) forControlEvents:UIControlEventTouchUpInside]; [_but3 setTitle:@"验证码" forState:UIControlStateNormal]; [self.view addSubview:_but3]; } //buton 按钮事件 -(void)butAction1:(UIButton * )sender{ UIAlertView * alter1 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"登陆成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"关闭", nil]; UIAlertView * alter2 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"登陆失败" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"关闭", nil]; //登陆 //用户名需在6到14位之内 密码需在6 位以上 验证码需要正确才可以成功登陆 否则用户提示重新登陆 NSString * str = [NSString stringWithFormat:@"%d%d%d%d",_a,_b,_c,_d]; //下面两种方法都可以获得 Button 按钮上的文字 // NSString * str1 = [NSString stringWithFormat:@"%@",_but3.titleLabel.text]; // NSString * str2 = [_but3 titleForState:UIControlStateNormal]; //titleForSatte 方法 获取 Button 在某一状态下的文字 NSLog(@"%@ %@",str,_tf3.text); if ((_tf2.text.length > 6)&&(_tf1.text.length >6 && _tf1.text.length < 14)&&([_tf3.text isEqualToString: str])) { [alter1 show]; [alter1 release]; //如果键盘还没有回收就把键盘回收掉 [self.tf resignFirstResponder]; }else{ [alter2 show]; [alter2 release]; [self.tf resignFirstResponder]; } } -(void)butAction2:(UIButton * )sender{ //取消登陆 _tf1.text = @""; _tf2.text = @""; _tf3.text = @""; } -(void)butAction3:(UIButton * )sender{ _a = arc4random()%10; _b = arc4random()%10; _c = arc4random()%10; _d =arc4random()%10; [_but3 setTitle:[NSString stringWithFormat:@"%d%d%d%d",_a,_b,_c,_d] forState:UIControlStateNormal]; } //自定义键盘视图 -(void)setUpCoustomKeyBoardView{ self.KeyView = [[UIView alloc]initWithFrame:CGRectMake(0, 400, 375, 240)]; //设置输入框的 inputView 在上面 //键盘辅助视图 self.accessaryView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 375, 20)]; _accessaryView.textAlignment = NSTextAlignmentCenter; _accessaryView.backgroundColor = [UIColor grayColor]; _accessaryView.textColor = RandomColor; _accessaryView.text = @"自定义辅助键盘"; //双重for循环,对于Button上的数字用二维数组 // NSArray * butArr[4][3] = {@[@"1",@"2",@"3"],@[@"4",@"5",@"6"],@[@"7",@"8",@"9"],@[@"✔️",@"0",@"?"]};错误 // NSArray * butArr[4][3] = @[@[@"1",@"2",@"3"],@[@"4",@"5",@"6"],@[@"7",@"8",@"9"],@[@"✔️",@"0",@"?"]];错误 NSArray * butArr = @[@[@"1",@"2",@"3"],@[@"4",@"5",@"6"],@[@"7",@"8",@"9"],@[@"✔️",@"0",@"?"]]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { UIButton * numButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; numButton.backgroundColor = [UIColor whiteColor]; //设置按钮文字 [numButton setTitle:[NSString stringWithFormat:@"%@",butArr[i][j]]forState:UIControlStateNormal]; //设置文字颜色 [numButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; //设置frame numButton.frame = CGRectMake(5 + ((375 -20)/3)*j, 30 *(1+i), (375-30)/3, 20); //为 Button 添加点击事件 (首先要把 一个完成键 一个清除键 单独添加方法) if (3 == i && 0 == j) { [numButton addTarget:self action:@selector(handleDoneAction:) forControlEvents:UIControlEventTouchUpInside];//完成键 的事件 }else if (3 == i && 2 == j) { //清除键 [numButton addTarget:self action:@selector(handleClearAction:) forControlEvents:UIControlEventTouchUpInside]; }else{ //其余按钮的点击事件 [numButton addTarget:self action:@selector(handleEditAction:) forControlEvents:UIControlEventTouchUpInside]; } [_KeyView addSubview:numButton]; } } // [self.view addSubview:_KeyView];不能这样写,这样写的话,在页面加载时候就把键盘显示了 // [_KeyView release]; } #pragma Mark --------- 键盘按钮方法----------- //确定键 -(void)handleDoneAction:(UIButton *)sender{ [self.tf resignFirstResponder]; } //清除键 -(void)handleClearAction:(UIButton *)sender{ NSMutableString * textString = [NSMutableString stringWithFormat:@"%@",self.tf.text]; if (self.tf.text.length != 0) {//判断字符串的长度 // [textString substringToIndex:self.tf.text.length - 1];//获取到前边几位字符串,然后再赋值给当前输入框 方法二 NSRange range = NSMakeRange(self.tf.text.length - 1, 1);//取出最后一个字符串 [textString deleteCharactersInRange:range];//删除最后一个字符串 self.tf.text = textString; } } //当编辑的时候进行保存 -(void)textFieldDidBeginEditing:(UITextField *)textField{ self.tf = textField; NSLog(@"textFieldDidBeginEditing %@",self.tf.text); } //空白区回收键盘 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.tf resignFirstResponder]; } //其余键 -(void)handleEditAction:(UIButton *)sender{ // NSMutableArray * string = nil;错误写法不能在下面接受到数值 一定是在堆区开辟的空间才行 // NSMutableString * string = [[NSMutableString alloc]init]; NSMutableString * string = [NSMutableString stringWithFormat:@"%@",self.tf.text]; //获取Button 某状态下的显示文字 titleForState NSString * numStr = [sender titleForState:UIControlStateNormal]; [string appendString:numStr]; self.tf.text = string; NSLog(@"string %@,numStr %@ ,self.tf.text %@",string,numStr,self.tf.text); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end