一.ViewController.h中代碼app
// // ViewController.h // QQRegLogin // // Created by jabez.huang on 15/6/15. // Copyright (c) 2015年 jabez.huang. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *QQNumber; @property (weak, nonatomic) IBOutlet UITextField *QQPassword; @property (weak, nonatomic) IBOutlet UILabel *ResultMessage; - (IBAction)Login:(id)sender; @end
二.ViewController.m中代碼atom
// // ViewController.m // QQRegLogin // // Created by jabez.huang on 15/6/15. // Copyright (c) 2015年 jabez.huang. All rights reserved. // #import "ViewController.h" #define PATH @"/Users/feifanchengxuyuan/Desktop/QQNumberFile.plist" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //設置QQ密碼文本框爲密碼框 _QQPassword.secureTextEntry = YES; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)Login:(id)sender { //注意:當文件裏屢次使用對象方法時,建議使用間接。 NSString *qqNumber = _QQNumber.text; NSString *qqPassword = _QQPassword.text; //判斷QQ號碼最小的位數爲5位 , QQ密碼最小的位數爲6位 if( ([qqNumber length] > 4 ) && ([qqPassword length] > 5)){ //建立一個NSFileManager對象 NSFileManager *fileManager = [NSFileManager defaultManager]; //建立一個文件流對象 NSData *data = [fileManager contentsAtPath: PATH]; //建立一個解檔 並鏈接文件流 NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData: data]; //經過相關的key解檔相關的QQ字典 NSDictionary *dict = [unArchiver decodeObjectForKey: @"QQ"]; //若是登陸輸入的qqNumber在字典裏找到了qqPassword也就是說,qqNumber 和 qqPassword輸入正確,也就是登陸成功 if ( [dict[qqNumber] isEqualToString: qqPassword] ){ _ResultMessage.text = @"登陸成功!"; }else{ _ResultMessage.text = @"QQ號碼或QQ密碼錯誤!"; } }else{ _ResultMessage.text = @"QQ號碼或QQ密碼錯誤!"; } } @end
三.RegViewController.hspa
// // RegViewController.h // QQRegLogin // // Created by jabez.huang on 15/6/15. // Copyright (c) 2015年 jabez.huang. All rights reserved. // #import <UIKit/UIKit.h> @interface RegViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *QQRegNumber; @property (weak, nonatomic) IBOutlet UITextField *QQRegPassword; @property (weak, nonatomic) IBOutlet UITextField *QQSureRegPassword; @property (weak, nonatomic) IBOutlet UILabel *ResultMessage; - (IBAction)QQRegButton:(id)sender; @end
四.RegViewController.mcode
// // RegViewController.m // QQRegLogin // // Created by jabez.huang on 15/6/15. // Copyright (c) 2015年 jabez.huang. All rights reserved. // #import "RegViewController.h" #define PATH @"/Users/feifanchengxuyuan/Desktop/QQNumberFile.plist" @interface RegViewController () @end @implementation RegViewController - (void)viewDidLoad { [super viewDidLoad]; //設置QQ號碼文本框不可用,由於是自動生成的 _QQRegNumber.enabled = NO; //設置QQ密碼文本框爲密碼框 _QQRegPassword.secureTextEntry = YES; //設置QQ確認密碼文本框爲密碼框 _QQSureRegPassword.secureTextEntry = YES; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ - (IBAction)QQRegButton:(id)sender { //QQ密碼和QQ確認密碼不能小於6位 if( [_QQRegPassword.text length] < 6 || [_QQSureRegPassword.text length] < 6){ _ResultMessage.text = @"密碼或確認密碼小於6位"; }else if( ![_QQRegPassword.text isEqualToString: _QQSureRegPassword.text ]){ //兩次輸入密碼不相等 _ResultMessage.text = @"兩次輸入密碼不相等!"; }else{ //根據當前時間戳獲得相關的QQ號 //注意:當申請人多的時候,建議使用毫秒來標識QQ號碼惟一性 NSDate *date = [NSDate date]; NSInteger QQNumber = (long)[date timeIntervalSince1970]; //把當前生成的QQ號碼寫入QQ號碼控件裏 _QQRegNumber.text = [NSString stringWithFormat: @"%li", QQNumber]; //提取相關的已有的帳號信息,防止新數據覆蓋老數據,把新數據和老數據合併組合加入字典裏 //建立一個NSFileManager對象 NSFileManager *fileManager = [NSFileManager defaultManager]; //建立一個NSData文件流對象 NSData *data = [fileManager contentsAtPath: PATH]; //建立一個解檔對象 NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData: data]; //定義一個可變的字典,給予一個初始的空間 NSMutableDictionary *mudict = [NSMutableDictionary dictionaryWithCapacity:10]; //把原始老的相關帳號數據放入字典臨時存儲 [mudict addEntriesFromDictionary: [unArchiver decodeObjectForKey: @"QQ"]]; //可變字典加入老數據 //如下爲新數據,注意:寫文件的時候的字典數據爲新數據+老數據 NSMutableData *mudata = [[NSMutableData alloc]init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData: mudata]; //歸檔寫入文件以前,先把新數據也加入可變字典,這樣字典裏目前就有了新數據 + 老數據 [mudict addEntriesFromDictionary:@{_QQRegNumber.text:_QQRegPassword.text} ]; //把組合好的(新數據+老數據) 歸檔 [archiver encodeObject: mudict forKey:@"QQ"]; //注意:必須寫finishEncoding,不然可能爲空 [archiver finishEncoding]; //歸檔之後寫入文件 [mudata writeToFile:PATH atomically:YES]; //顯示提示信息 _ResultMessage.text = @"註冊成功!"; // NSLog( @"%@", [unArchiver decodeObjectForKey: @"QQ"] ); } } @end
五.跳轉下一頁面
添加頁面View Controller,而後用Button按鈕選擇show,一樣返回也用Button按鈕選擇show
六.跳轉頁面設置orm
選擇右側Custon Class中Class中的ViewController對象
或者新建一個,iOS->Source->Cocoa Touch Class選擇UIViewControllerci