標籤:des style blog http io color ar os 使用
iOS開發UI篇—實現一個私人通訊錄小應用(一)
一、該部分主要完成內容
1.介面搭建
2.功能說明
(1).只有當帳號和密碼輸入框都有值的時候,登入按鈕才能互動
(2).當取消勾選記住密碼後,自動登入按鈕也隨之取消;當勾選了自動登入按鈕時,記住密碼按鈕也一同勾選。
(3).點擊登陸後,彈出蒙版,介面不可互動,程式能夠簡單判斷帳號和密碼是否正確,如果不正確則給出相應的提示,如果正確則跳轉到連絡人清單介面。
二、實現過程和代碼
專案檔結構圖和介面搭建
實現代碼:
TXloginViewController.m檔案
1 // 33-梁钂鑫通訊錄(登入) 2 // 3 // Created by 鑫 on 14-10-21. 4 // Copyright (c) 2014年 梁钂鑫. All rights reserved. 5 // 6 7 #import "TXLoginViewController.h" 8 #import "MBProgressHUD+MJ.h" 9 @interface TXLoginViewController () 10 @property (weak, nonatomic) IBOutlet UITextField *accountField; 11 @property (weak, nonatomic) IBOutlet UITextField *pwField; 12 @property (weak, nonatomic) IBOutlet UIButton *loginBtn; 13 @property (weak, nonatomic) IBOutlet UISwitch *rmbPwdSwitch; 14 15 - (IBAction)rmbPwdChange; 16 @property (weak, nonatomic) IBOutlet UISwitch *autoLoginSwitch; 17 18 - (IBAction)autoLoginChange; 19 - (IBAction)login; 20 21 @end 22 23 @implementation TXLoginViewController 24 25 26 - (void)viewDidLoad 27 { 28 [super viewDidLoad]; 29 30 31 32 33 34 //監聽通知 控制器self去監聽 name通知名稱 35 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.accountField]; 36 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.pwField]; 37 38 } 39 //移除監聽 40 -(void)dealloc 41 { 42 [[NSNotificationCenter defaultCenter] removeObserver:self]; 43 } 44 /** 45 * 文字框的文字發生改變時調用 46 */ 47 -(void)textChange 48 { 49 //控制器按鈕狀態 50 // if (self.accountField.text.length>0&&self.pwField.text.length>0) { 51 // self.loginBtn.enabled = YES; 52 // } 53 // else 54 // { 55 // self.loginBtn.enabled = NO; 56 // } 57 //簡化後 58 59 self.loginBtn.enabled = (self.accountField.text.length&& self.pwField.text.length); 60 } 61 - (IBAction)rmbPwdChange { 62 //取消記住密碼 63 if (self.rmbPwdSwitch.isOn ==NO) { 64 self.autoLoginSwitch.on = NO; 65 } 66 } 67 //自動登入的狀態發生改變 68 - (IBAction)autoLoginChange { 69 70 if (self.autoLoginSwitch.isOn) { 71 self.rmbPwdSwitch.on=YES; 72 } 73 74 } 75 76 //登陸 77 //HUD指標 78 79 - (IBAction)login { //帳號tx 密碼 123 80 81 if (![self.accountField.text isEqualToString:@"mj"]) { 82 // 帳號不存在 83 [MBProgressHUD showError:@"帳號不存在"]; 84 return; 85 } 86 87 if (![self.pwField.text isEqualToString:@"123"]) { 88 // 密碼錯誤 89 [MBProgressHUD showError:@"密碼錯誤"]; 90 return; 91 } 92 93 // 顯示一個蒙版(遮蓋) 94 [MBProgressHUD showMessage:@"哥正在幫你登入中...."]; 95 96 // 發送網路請求 97 98 // 類比(2秒後執行跳轉) 99 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{100 // 移除遮蓋101 [MBProgressHUD hideHUD];102 103 // 跳轉 -- 執行login2contacts這個segue104 [self performSegueWithIdentifier:@"login2contacts" sender:nil];105 });106 107 }108 109 110 @end
三、segue的使用1.簡單介紹
Storyboard上每一根用來介面跳轉的線,都是一個UIStoryboardSegue對象(簡稱Segue)
2.segue的三個屬性
每一個Segue對象,都有3個屬性
唯一標識 @property (nonatomic, readonly) NSString *identifier;
來源控制器 @property (nonatomic, readonly) id sourceViewController;
目標控制器 @property (nonatomic, readonly) id destinationViewController;
2.segue的兩種類型
根據Segue的執行(跳轉)時刻,Segue可以分為2大類型
自動型:點擊某個控制項後(比如按鈕),自動執行Segue,自動完成介面跳轉
按住Control鍵,直接從控制項拖線到目標控制器。點擊“登入”按鈕後,就會自動跳轉到右邊的控制器。如果點擊某個控制項後,不需要做任何判斷,一定要跳轉到下一個介面,建議使用“自動型Segue”
手動型:需要通過寫代碼手動執行Segue,才能完成介面跳轉
按住Control鍵,從來源控制器拖線到目標控制器。手動型的Segue需要設定一個標識()。
在恰當的時刻,使用perform方法執行對應的Segue [self performSegueWithIdentifier:@"login2contacts" sender:nil];
Segue必須由來源控制器來執行,也就是說,這個perform方法必須由來源控制器來調用。如果點擊某個控制項後,需要做一些判斷,也就是說:滿足一定條件後才跳轉到下一個介面,建議使用“手動型Segue”
3.performSegueWithIdentifier:sender:
利用performSegueWithIdentifier:方法可以執行某個Segue,完成介面跳轉
4.performSegueWithIdentifier:sender:方法的完整執行過程
[selfperformSegueWithIdentifier:@“login2contacts”sender:nil];
(1)根據identifier去storyboard中找到對應的線,建立UIStoryboardSegue對象
設定Segue對象的sourceViewController(來源控制器)
建立並且設定Segue對象的destinationViewController(目標控制器)
(2)調用sourceViewController的下面方法,做一些跳轉前的準備工作並且傳入建立好的Segue對象
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;
// 這個sender是當初performSegueWithIdentifier:sender:中傳入的sender
(3)調用Segue對象的- (void)perform;方法開始執行介面跳轉操作
取得sourceViewController所在的UINavigationController
調用UINavigationController的push方法將destinationViewController壓入棧中,完成跳轉
4.多個控制器間資料的傳遞
(1)順傳:從當前控制器將資料傳遞到下一個控制器
(2)逆傳:從當前控制器將資料傳遞給前面的控制器
iOS開發UI篇—實現一個私人通訊錄小應用(一)