持久化傳值
開發中,有很多數據持久化的方案,比如接下來我們就介紹一下6中方案中的NSUserDefaults:
Plist文件 (屬性列表)
Preference (偏好設置)
NSKeyedArchiver (歸檔)
SQLite 3
CoreData
NSUserDefaults (數據持久化)
NSUserDefaults
一般用在保存賬號,密碼,登錄狀態,針對小數據,如果大了不讓上線
所謂的持久化,就是將數據保存到硬盤中,使得在應用程序或機器重啟后可以繼續訪問之前保存的數據
詳細地址:http://www.cocoachina.com/ios/20150720/12610.html
- 使用方式:
//1、在NextViewController.m中的返回上一頁方法中 使用持久化傳值
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setObject:self.textField1.text forKey:@"haha"];
//2、在RootViewController.m中:這里注意必須要寫到 viewWillApper 的方法里面
-(void)viewWillAppear:(BOOL)animated{
//持久化傳值
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
self.textField.text = [user objectForKey:@"haha"];
}
補充說明:http://www.ithao123.cn/content-7906829.html
持久化傳值 正向傳值也是一樣 也是1、2
IOS單例模式(Singleton)
- 單例模式的意思就是只有一個實例,單例模式確保某一個類只有一個實例,而且自行實例化并向整個系統提供這個實例,這個類稱為單例類。
- 如果說創建一個對象會耗費很多系統資源,那么此時采用單例模式,因為只需要一個實例,會節省alloc的時間.
- 在IOS開發中,如果很多模塊都要使用同一個變量,此時如果把該變量放入單例類,則所有訪問該變量的調用變得很容易,否則,只能通過一個模塊傳遞給另外一個模塊,這樣增加了風險和復雜度
單例模式的使用場景:
在整個應用程序中,共享一份資源(這份資源只需要創建初始化一次)1、單例模式的要點:
顯然單例模式的要點有三個:
1)某個類只能有一個實例
2)它必須自行創建這個實例
3)它必須自行向整個系統提供這個實例。2、單例模式的優點:
1)實例控制:Singleton 會阻止其他對象實例化其自己的 Singleton 對象的副本,從而確保所有對象都訪問唯一實例。
2)靈活性:因為類控制了實例化過程,所以類可以更加靈活的修改實例化過程IOS單例模式
在OC中實例一個單例類,至少需要做到以下四個步驟:
1、為單例對象實現一個靜態實例,并初始化,然后設置成nil
2、實現一個實例構造方法檢查上面聲明的靜態實例是否為nil,如果是則新建并返回一個本類的實例
3、重寫allocWithZone方法,用來保證其他人直接使用alloc和init視圖獲得一個新實力的時候不產生一個新實例
4、適當實現allocWitheZone,copyWithZone,release和autorelease。
判斷是否是單例的方法,首先他是類方法,其次我們看到方法為share 或者是 defert的就是單例
單例傳值(重點):不需要傳遞任何參數,有效解決不同代碼的數據共享問題
單例類保證了應用程序的生命周期有且僅有一個該類的實例對象,而且易于外界訪問。
- 1、我們先創建一個繼承于NSObject的文件 segleHandle
在 segleHandle.h中, 我們定義一個類方法調用,并定義2個傳值用的屬性
@interface segleHandle : NSObject
//傳值用的屬性
+(instancetype)shareSegleHandle;
@property(nonatomic,strong)NSString *frontString;
@property(nonatomic,strong)NSString *imageStr;
@property(nonatomic,strong)NSString *backString;
@end
在 segleHandle.m中, 直接輸入dispatch_once_t 看到GCD,回車
@implementation segleHandle
//靜態區static segleHandle *handle = nil;
//線程,這個block中的代碼 在程序期間只會執行一次
+(instancetype)shareSegleHandle{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
handle = [[segleHandle alloc] init];
});
return handle;
}
@end
上面的block代碼實現了以下這個功能,所以下面的不用寫
+(instancetype)shareSegleHandle{// 判斷 保證唯一性
if (nil ==handle) {
handle = [[segleHandle alloc] init];
} return handle;
}
- 使用系統的單例傳值:
使用步驟:
1.引用
#import "AppDelegate.h"
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//這里的 appDelegate 就是 appDelegate.findView = findV;
//使用系統的單例傳值
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
FindView *findV = appDelegate.findView;
第二種單例模式:
在我們使用單例的時候,為了確保類的唯一性,所以一般重寫alloc with zone。完善的代碼如下:
#import <Foundation/Foundation.h>
@interface ZJPerson : NSObject
+(instancetype)sharePerson;
@end
.m文件如下:=========================================
#import "ZJPerson.h"
@interface ZJPerson ()<NSCopying> **//如果寫了copy這里的NSCopying可以不寫**
@end
@implementation ZJPersonstatic ZJPerson *_person;
//調用一千次也是返回的同一個對象,不管調用多少次alloc返回的也是Person
+(instancetype)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_person = [super allocWithZone:zone];
});
return _person;
}
//不管調用多少次sharePerson保證創建初始化1個對象
//不管調用多少次sharePerson,始終初始化一次。alloc init一次
+(instancetype)sharePerson{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_person = [[self alloc] init];
});
return _person;
}
//在別人調用copy的時候,返回的是同一個對象
-(id)copyWithZone:(NSZone *)zone{
return _person;
}
@end
單例正向傳值:
1.在RootViewController.m中的點擊下一頁的方法中:
segleHandle *segle = [segleHandle shareSegleHandle];
segle.frontString = self.textField.text;
segle.imageStr = @"1.jpg";
2、在NextViewController.m中:
-(void)viewWillAppear:(BOOL)animated{
self.textField1.text = [segleHandle shareSegleHandle].frontString;
self.nextImageV.image = [UIImage imageNamed:[segleHandle shareSegleHandle].imageStr];
}
單例逆向傳值:
1.在NextViewController.m中的返回上一頁的方法中
segleHandle *seg = [segleHandle shareSegleHandle];
seg.backString = self.textField1.text;
2、在RootViewController.m中
self.textField.text = [segleHandle shareSegleHandle].backString;
文件傳值:
本地持久化(離線的工作,把服務器取回來的數據的數據存在本地沙盒目錄下,然后下次直接讀文件取用。任何情況下,你有一個數據想給別的類用,可以把這個數據 write到文件,然后別的類讀這個文件)
通知傳值:(重點)一個發起者,一堆接收者,不需要協議,直接通過系統的通知中心工作
誰要監聽值的變化,誰就注冊通知 特別要注意,通知的接受者必須存在這一先決條件
- notification Center 一對多
在傳值頁面執行如下代碼發通知:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//第二個參數object是我們要傳的值,接受那邊會受到這個值。這個參數必須是對象。[NSString stringWithFormat:@"%d",num]就是發出去的值.
NSNotification *notify = [NSNotification notificationWithName:@"zengzhiwei" object:[NSString stringWithFormat:@"%d",num]];
[center postNotification:notify];
在接收頁面:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(secondDoWithNotification:) name:@"zengzhiwei" object:nil];
//第一個參數是目標(將要干活的對象),第二個參數是方法(要干的活),第三個參數是我正在觀察的內容,要與發出者一致,第四個參數是觀察者的參數,是給中心用的,我們暫時不關心。
//因為這次發送的zengzhiwei是帶參數的,(執行對應方法。)
-(void)secondDoWithNotification:(NSNotification *)notice {
NSString *object = [notice object];//把收到的通知參數轉為字符串。
if([object intValue ] == 0 ){
//刪上面的按鈕
UIButton *btn = (UIButton *)[self.view viewWithTag:1000];
[btn removeFromSuperview];
}else{
//刪下面的按鈕
UIButton *btn = (UIButton *)[self.view viewWithTag:1001];
[btn removeFromSuperview];
}
}
**繼續通知傳值: **
// 發送通知 修改mainView界面的名字
NSDictionary *infoDic = [NSDictionary dictionaryWithObjectsAndKeys:alertCtrl.textFields[0].text,@"name",nil];
[[NSNotificationCenter defaultCenter]postNotificationName:kNotificationChangeUserName object:nil userInfo:infoDic];
===========================================================
另一個控制器里面寫:
// 注冊修改用戶名稱通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationChangeUserNameAction:) name:kNotificationChangeUserName object:nil];
// 用戶修改名稱
- (void)notificationChangeUserNameAction:(NSNotification *)notification {
NSString *iconButtonTitle = [notification.userInfo objectForKey:@"name"];
[self.dock.iconButton setTitle:iconButtonTitle forState:UIControlStateNormal];
}
#pragma mark - 沒有調用
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}