首先需要導(dǎo)入一個(gè)第三方的SDK? ? MBProgressHUD 拔创,為了顯示提示框,如下圖:
在viewController中寫(xiě)控件的屬性代碼:
@property(nonatomic,strong)UITextField * Account;
@property(nonatomic,strong)UITextField * passWord;
@property(nonatomic,strong)UIButton * Login;
@property(nonatomic,strong)UIButton * Register;
@property(nonatomic,strong)UISwitch * Switch;
利用封裝的方法富蓄,加載控件的初始化方法
//初始化賬號(hào)輸入框
-(UITextField*)Account
{
? ? if(!_Account)
? ? {
? ? ? ? _Account=[[UITextField alloc]initWithFrame:CGRectMake(5, 280, self.view.frame.size.width-10, 50)];
? ? ? ? _Account.borderStyle=UITextBorderStyleRoundedRect;
? ? ? ? _Account.alpha=0.5;
? ? ? ? _Account.placeholder=@"請(qǐng)輸入賬號(hào)";
? ? ? ? _Account.textAlignment=NSTextAlignmentCenter;
? ? ? ? _Account.keyboardType=UIKeyboardTypeNamePhonePad;
? ? }
? ? return _Account;
}
//初始化密碼輸入框
-(UITextField* )passWord
{
? ? if(!_passWord)
? ? {
? ? ? ? _passWord=[[UITextField alloc]initWithFrame:CGRectMake(5, 350, self.view.frame.size.width-10, 50)];
?? ? ? ? _passWord.borderStyle=UITextBorderStyleRoundedRect;
? ? ? ? _passWord.alpha=0.5;
? ? ? ? _passWord.placeholder=@"請(qǐng)輸入密碼";
? ? ? ? _passWord.secureTextEntry=YES;
? ? ? ? _passWord.textAlignment=NSTextAlignmentCenter;
? ? ? ? _passWord.keyboardType=UIKeyboardTypePhonePad;
? ? }
? ? return _passWord;
}
//初始化登錄按鈕
-(UIButton* )Login
{
? ? if(!_Login)
? ? {
? ? ? ? _Login=[[UIButton alloc]initWithFrame:CGRectMake(20,450,self.view.frame.size.width-40,50)];
? ? ? ? _Login.backgroundColor=[UIColor orangeColor];
? ? ? ? _Login.alpha=0.8;
? ? ? ? _Login.layer.cornerRadius=20;
? ? ? ? _Login.layer.masksToBounds=YES;
? ? ? ? [_Login setTitle:@"登錄" forState:UIControlStateNormal];
?? ? ? ? [_Login addTarget:self action:@selector(denglu) forControlEvents:UIControlEventTouchUpInside];
? ? }
? ? return _Login;
}
//初始化注冊(cè)按鈕
-(UIButton* )Register
{
? ? if(!_Register)
? ? {
? ? ? ? _Register=[[UIButton alloc]initWithFrame:CGRectMake(350,750,self.view.frame.size.width-350,40)];
? ? ? ? [_Register setTitle:@"注冊(cè)" forState:UIControlStateNormal];
? ? ? ? [_Register setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
? ? ? ? [_Register addTarget:self action:@selector(zhuce) forControlEvents:UIControlEventTouchUpInside];
?? ? ? // _Register.backgroundColor=[UIColor redColor];
? ? }
? ? return _Register;
}
//初始化開(kāi)關(guān)
-(UISwitch* )Switch
{
? ? if(!_Switch)
? ? {
? ? ? ? _Switch=[[UISwitch alloc]initWithFrame:CGRectMake(5, 750, 100, 40)];
? ? ? ? [_Switch addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventValueChanged];
? ? }
? ? return _Switch;
}
在viewDidLoad中添加控件到主視圖:
//添加賬號(hào)文本框
? ? [self.view? addSubview:self.Account];
? ? //添加密碼文本框
? ? [self.view? addSubview:self.passWord];
? ? //添加登錄按鈕
? ? [self.view? addSubview:self.Login];
? ? //添加注冊(cè)按鈕
? ? [self.view? addSubview:self.Register];
? ? //添加開(kāi)關(guān)
? ? [self.view? addSubview:self.Switch];
可以設(shè)置一些界面的效果:
//導(dǎo)航不透明
? ? self.navigationController.navigationBar.translucent=NO;
? ? self.view.backgroundColor=[UIColor whiteColor];
? ? //標(biāo)題
? ? self.navigationItem.title=@"登錄界面";
? ? //背景視圖
? ? UIImageView * imgv=[[UIImageView alloc]initWithFrame:self.view.frame];
? ? imgv.image=[UIImage? imageNamed:@"16.jpeg"];
? ? imgv.alpha=0.5;
? ? [self.view? addSubview:imgv];
? ? //頭像視圖
? ? UIImageView * touImgv=[[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-100)/2, 100, 100, 100)];
? ? touImgv.layer.cornerRadius=50;
? ? touImgv.layer.masksToBounds=YES;
? ? touImgv.image=[UIImage imageNamed:@"12.jpg"];
? ? [self.view? addSubview:touImgv];
寫(xiě)點(diǎn)擊和注冊(cè)的方法:
//實(shí)現(xiàn)注冊(cè)點(diǎn)擊方法
-(void)zhuce
{
? ? ZCViewController * zc=[ZCViewController new];
? ? [self.navigationController pushViewController:zc animated:YES];
}
//實(shí)現(xiàn)登錄點(diǎn)擊方法
-(void)denglu
{
? ? //創(chuàng)建NSUserDefaults對(duì)象
? ? NSUserDefaults * userDes=[NSUserDefaults standardUserDefaults];
? ? //獲取NSUserDefaults的賬戶
? ? NSString* user = [userDes objectForKey:@"account"];
? ? NSString* pass=[userDes objectForKey:@"password"];
? ? //判斷是否成功? 如果user剩燥,pass和輸入框的文字相同,則登錄成功立倍,否則登錄失敗
? ? if([self.Account.text isEqualToString:user] &&[self.passWord.text isEqualToString:pass] )
? ? {
? ? ? ? //提示用戶登錄成功
? ? ? ? [self showMBAlertWithMessage:@"登錄成功"];
? ? }
? ? else
? ? {
? ? ? ? //提示用戶登錄失敗
? ? ? ? [self showMBAlertWithMessage:@"賬號(hào)或密碼有誤灭红,請(qǐng)重新輸入"];
? ? }
}
//實(shí)現(xiàn)開(kāi)關(guān)的方法
-(void)changeColor:(UISwitch*)Switch{
? ? if(Switch.isOn){
? ? ? ? NSLog(@"開(kāi)");
? ? }else{
? ? ? ? NSLog(@"關(guān)");
? ? }
}
//點(diǎn)擊屏幕的空白處收起鍵盤(pán)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
{
? ? [self.view endEditing:YES];
}
// 設(shè)置第三方的顯示提示框
-(void)showMBAlertWithMessage:(NSString*)msg {
? ? MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
? ? hud.mode = MBProgressHUDModeText; // 設(shè)置文本樣式
? ? hud.labelText= msg;? ? // 設(shè)置顯示的提示文本
? ? hud.removeFromSuperViewOnHide = YES;
? ? [self.view addSubview:hud];
? ? [hud show:YES];
? ? [hud hide:YES afterDelay:3.0];
}
我們上面代碼中寫(xiě)到跳轉(zhuǎn)頁(yè)面到ZCViewController,所以我們需要?jiǎng)?chuàng)建一個(gè)繼承與UIViewcontroller的視圖控制器:
聲明屬性:
@property(nonatomic,strong)UITextField * Account;
@property(nonatomic,strong)UITextField * passWord;
@property(nonatomic,strong)UIButton * Register;
初始化控件:
//初始化賬號(hào)輸入框
-(UITextField*)Account
{
? ? if(!_Account)
? ? {
? ? ? ? _Account=[[UITextField alloc]initWithFrame:CGRectMake(5, 100, self.view.frame.size.width-10, 50)];
? ? ? ? _Account.borderStyle=UITextBorderStyleRoundedRect;
? ? ? ? _Account.alpha=0.5;
? ? ? ? _Account.placeholder=@"請(qǐng)輸入賬號(hào)";
? ? ? ? _Account.textAlignment=NSTextAlignmentCenter;
? ? ? ? _Account.keyboardType=UIKeyboardTypeNamePhonePad;
? ? }
? ? return _Account;
}
//初始化密碼輸入框
-(UITextField* )passWord
{
? ? if(!_passWord)
? ? {
? ? ? ? _passWord=[[UITextField alloc]initWithFrame:CGRectMake(5, 160, self.view.frame.size.width-10, 50)];
? ? ? ? _passWord.borderStyle=UITextBorderStyleRoundedRect;
? ? ? ? _passWord.alpha=0.5;
? ? ? ? _passWord.placeholder=@"請(qǐng)輸入密碼";
? ? ? ? _passWord.secureTextEntry=YES;
? ? ? ? _passWord.textAlignment=NSTextAlignmentCenter;
? ? ? ? _passWord.keyboardType=UIKeyboardTypePhonePad;
? ? }
? ? return _passWord;
}
//初始化注冊(cè)按鈕
-(UIButton* )Register
{
? ? if(!_Register)
? ? {
? ? ? ? _Register=[[UIButton alloc]initWithFrame:CGRectMake(20,400,self.view.frame.size.width-40,40)];
? ? ? ? [_Register setTitle:@"注冊(cè)" forState:UIControlStateNormal];
? ? ? ? [_Register setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
? ? ? ? [_Register addTarget:self action:@selector(zhuce) forControlEvents:UIControlEventTouchUpInside];
?? ? ? ? _Register.backgroundColor=[UIColor greenColor];
? ? ? ? _Register.layer.cornerRadius=20;
? ? ? ? _Register.layer.masksToBounds=YES;
? ? ? ? [_Register setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
? ? return _Register;
}
在viewDidLoad中寫(xiě):
//設(shè)置頁(yè)面的背景顏色
? ? self.view.backgroundColor=[UIColor whiteColor];
? ? //設(shè)置標(biāo)題
? ? self.navigationItem.title=@"注冊(cè)";
? ? //添加賬號(hào)文本框
? ? [self.view addSubview:self.Account];
? ? //添加密碼文本框
? ? [self.view addSubview:self.passWord];
? ? //添加注冊(cè)按鈕
? ? [self.view addSubview:self.Register];
接下里實(shí)現(xiàn)點(diǎn)擊注冊(cè)的方法:
//實(shí)現(xiàn)注冊(cè)點(diǎn)擊方法
-(void)zhuce
{
? ? //判斷是否為空
? ? if(self.Account.text.length!=0 && self.passWord.text.length!=0)
? ? {
? ? ? ? //獲取輸入框的內(nèi)容
? ? ? ? NSString* userStr=self.Account.text;
? ? ? ? NSString* passStr=self.passWord.text;
? ? ? ? //將獲取的文字? 存儲(chǔ)到NSUserDefaults
? ? ? ? NSUserDefaults * userDes=[NSUserDefaults standardUserDefaults];
? ? ? ? //存儲(chǔ)數(shù)據(jù)
? ? ? ? [userDes? setObject:userStr? forKey:@"account"];
? ? ? ? [userDes? setObject:passStr? forKey:@"password"];
? ? ? ? //提示用戶注冊(cè)成功
? ? ? ? [self showMBAlertWithMessage:@"注冊(cè)成功"];
? ? }
? ? else
? ? {
? ? ? ? //提示用戶輸入內(nèi)容不能為空
? ? ? ? [self showMBAlertWithMessage:@"輸入內(nèi)容不能為空"];
? ? }
}
// 設(shè)置第三方的顯示提示框
-(void)showMBAlertWithMessage:(NSString*)msg {
? ? MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
? ? hud.mode = MBProgressHUDModeText; // 設(shè)置文本樣式
? ? hud.labelText= msg;? ? // 設(shè)置顯示的提示文本
? ? hud.removeFromSuperViewOnHide = YES;
? ? [self.view? addSubview:hud];
? ? [hud? show:YES];
? ? [hud? hide:YES afterDelay:3.0];
}
//點(diǎn)擊屏幕的空白處收起鍵盤(pán)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
{
? ? [self.view endEditing:YES];
}