iOS 用戶登錄界面

jopen 10年前發布 | 52K 次閱讀 iOS開發 移動開發 IOS

界面由代碼生成,輸入合法性驗證

導航

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;

@end

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;

@end

ViewController.m

import "ViewController.h"

@interface ViewController (){

NSString *namelabel;

NSString *passLab;

NSString *checkPassLab;

NSString *emailLab;

NSString *numberLab;

NSString *sexLab;

}

@end

@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=@"";

    }

}



//email

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];

}

}</pre></span>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!