导航
AppDelegate.h
AppDelegate.m
ViewController.h
ViewController.m
userEnterView.xib
#AppDelegate.h
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property(nonatomic,retain)ViewController *vc;
@property (strong, nonatomic) UIWindow *window;
#AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
CGRect rect=[[UIScreen mainScreen]bounds];
self.window=[[UIWindow alloc]initWithFrame:rect];
self.vc=[[ViewController alloc]initWithNibName:@"userEnterView" bundle:nil];
self.window.rootViewController=self.vc;
[self.window makeKeyAndVisible];
return YES;
}
#ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>
@property(nonatomic,retain)NSArray *info;
@property(nonatomic,retain)NSMutableArray *checkinfo;
@property(nonatomic,retain)NSMutableArray *textfiledArrary;
-(void)textField_DidEndExit:(id)sender;
-(void)commitAllInfo;
-(void)drawboard;
#ViewController.m
#import "ViewController.h"
@interface ViewController (){
NSString *namelabel;
NSString *passLab;
NSString *checkPassLab;
NSString *emailLab;
NSString *numberLab;
NSString *sexLab;
}
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//用于显示的label
namelabel=@"Name:";
passLab=@"Pass:";
checkPassLab=@"RePass:";
emailLab=@"Email:";
numberLab=@"Number:";
sexLab=@"Sex:";
//数组的初始化
self.info=[[NSArray alloc]initWithObjects:namelabel,passLab,checkPassLab,emailLab,numberLab,sexLab,nil];
self.checkinfo=[[NSMutableArray alloc]init];
self.textfiledArrary=[[NSMutableArray alloc]init];
//绘制界面
[self drawboard];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)drawboard{
for (int i=0; i<6; i++) {
//显示Label
UILabel *label=[[UILabel alloc]init];
label.frame=CGRectMake(40, (i+1)*50, 50, 20);
[label setTextColor:[UIColor blackColor]];
label.textAlignment=UITextAlignmentRight;
label.adjustsFontSizeToFitWidth=YES;
label.text=[self.info objectAtIndex:i];
//输入框
UITextField *textfield=[[UITextField alloc]init];
textfield.frame=CGRectMake(100, (i+1)*50, 120, 20);
textfield.borderStyle=UITextBorderStyleRoundedRect;
textfield.tag=i;
if (textfield.tag==1||textfield.tag==2) {
textfield.secureTextEntry=YES;
}
//添加委托
textfield.delegate=self;
//给软键盘添加退出
[textfield addTarget:self action:@selector(textField_DidEndExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.textfiledArrary addObject:textfield];
//检测label
UILabel *labelCheck=[[UILabel alloc]init];
labelCheck.frame=CGRectMake(240, (i+1)*50, 50, 20);
labelCheck.adjustsFontSizeToFitWidth=YES;
labelCheck.tag=i;
[self.checkinfo addObject:labelCheck];
[self.view addSubview:labelCheck];
[self.view addSubview:textfield];
[self.view addSubview:label];
}
//提交按钮
UIButton *button=[[UIButton alloc]init];
button.backgroundColor=[UIColor lightGrayColor];
button.frame=CGRectMake(100, 350, 120, 30);
[button setTitle:@"注册" forState:UIControlStateNormal];
[button addTarget:self action:@selector(commitAllInfo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
NSString *str=textField.text;
//检测密码
if (textField.tag==2) {
//
UITextField *textCheck=(UITextField *)[self.textfiledArrary objectAtIndex:1];
UILabel *label=(UILabel *)[self.checkinfo objectAtIndex:2];
if (![str isEqualToString:textCheck.text]) {
label.text=@"密码不一致";
}
if ([str isEqualToString:textCheck.text]) {
label.text=@"";
}
}
if (textField.tag==3) {
NSString *emailCheck=@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest=[NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailCheck];
if (![emailTest evaluateWithObject:str]) {
UILabel *label=(UILabel *)[self.checkinfo objectAtIndex:3];
label.text=@"格式错误";
}
else{
UILabel *label=(UILabel *)[self.checkinfo objectAtIndex:3];
label.text=@"";
}
}
//号码有效性
if (textField.tag==4) {
NSString *phoneCheck=@"^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
NSPredicate *phonelTest=[NSPredicate predicateWithFormat:@"SELF MATCHES%@",phoneCheck];
if (![phonelTest evaluateWithObject:str]) {
UILabel *label=(UILabel *)[self.checkinfo objectAtIndex:4];
label.text=@"格式错误";
}
else{
UILabel *label=(UILabel *)[self.checkinfo objectAtIndex:4];
label.text=@"";
}
}
//键盘遮挡输入框
self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
-(void)textField_DidEndExit:(id)sender
{
[sender resignFirstResponder];
}
//软键盘遮挡屏幕
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect frame=textField.frame;
int offset=frame.origin.y+32-(self.view.frame.size.height-216.0);
NSTimeInterval animationDuration=0.30f;
[UIView beginAnimations:@"ReSizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
if(offset > 0)
self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
-(void)commitAllInfo
{
for (id textIno in self.textfiledArrary) {
UITextField *tempText=(UITextField *)textIno;
[self textFieldDidEndEditing:tempText];
}
}