1.1.3.使用通知傳值
1.2.Segue使用
2. KVC&&KVO2.1.什么是KVC
KVC - Key Value Coding鍵值(路徑)編碼KVC是一種間接修改/讀取對(duì)象屬性的一種方式KVC被稱為蘋果開發(fā)的大招!
KVC在使用時(shí),需要注意,鍵值名稱在對(duì)象屬性中必須存在,否則會(huì)崩潰!
2.2.什么是KVO
Key Value Observer鍵值觀察(觀察者模式)
通知中心同樣也是觀察者模式
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......2.3.KVC設(shè)置值
2.3.1.給字符串屬性 和 數(shù)值屬性賦值
Person*p = [[Personalloc]init];//使KVC來給對(duì)象賦值
[psetValue:@"李四"forKeyPath:@"name"];
//給數(shù)值型屬性賦值時(shí),同樣可以使 字符串[psetValue:@"18"forKeyPath:@"age"];
2.3.2.給對(duì)象屬性賦值
Student*stu = [[Studentalloc]init];
[stusetValue:@"李四"forKeyPath:@"name"];
[stusetValue:@"18"forKeyPath:@"age"];
[stusetValue:@"20"forKeyPath:@"number"];Book*book = [[Bookalloc]init];
stu.book= book;
//使KVC給Book賦值
[stusetValue:@"iPhone"forKeyPath:@"book.bookName"];
2.4.KVC取值
Student*stu = [[Studentalloc]init];
[stusetValue:@"lisi"forKeyPath:@"name"];
[stusetValue:@"18"forKeyPath:@"age"];
[stusetValue:@"20"forKeyPath:@"number"];
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......
Book*book = [[Bookalloc]init];
stu.book= book;
[stusetValue:@"iPhone"forKeyPath:@"book.bookName"];
Student*stu1 = [[Studentalloc]init];
[stu1setValue:@"wangwu"forKeyPath:@"name"];
[stu1setValue:@"18"forKeyPath:@"age"];
[stu1setValue:@"20"forKeyPath:@"number"];Book*book1 = [[Bookalloc]init];
stu1.book= book1;
[stu1setValue:@"ios"forKeyPath:@"book.bookName"];
NSArray*students =@[stu,stu1];//需求,將兩個(gè)學(xué) 的姓名 成 個(gè)數(shù)組
//普通做法
//
//
//
//
//
//
//
//
NSMutableArray *arrayM = [NSMutableArray array];
for (Student *stu in students) {
[arrayM addObject:stu.name];
}
NSLog(@"%@", arrayM);
KVC取值
使KVC取值的時(shí)候,如果當(dāng)前對(duì)象不包含指定的鍵值
那么KVC會(huì)進(jìn) 對(duì)象內(nèi)部,繼續(xù)搜索!
NSLog(@"%@", [studentsvalueForKeyPath:@"name"]);NSLog(@"%@", [studentsvalueForKeyPath:@"book.bookName"]);
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......2.5.KVO的使用
2.5.1.設(shè)置監(jiān)聽
Student *stu = [[Student alloc] init];
stu.name =@"wangwu";
stu.age =16;
stu.number =20;
Book *book = [[Book alloc] init];
book.bookName =@"ios";
stu.book = book;self.student =stu;
//誰來監(jiān)聽 監(jiān)聽誰
//觀察student的屬性變化
// 1> self負(fù)責(zé)監(jiān)聽的對(duì)象
通知中 負(fù)責(zé)
NSNotificationCenter是由
// 2> keyPath要觀察的屬性鍵值路徑,KVO主要 于對(duì)模型屬性變化進(jìn)
觀察的 種模式
//通知中 是通過對(duì)象POST字符串給通知中 來達(dá)到觀察 的的
// 3>選項(xiàng)"|"(并且)位運(yùn)算,可以同時(shí) 持多個(gè)選項(xiàng)
// 4>上下
[stu addObserver:selfforKeyPath:@"name"options:NSKeyValueObservingOptionNew|NSKeyValueObservingO
ptionOld context:@"student"];
stu.name =@"lisi";
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......2.5.2.監(jiān)聽代理
//分類: 被稱為:隱式代理,靈活度很 ,但是代碼的可讀性不好,不建議
使 !
// ?delegate ->顯式代理//
//觀察監(jiān)聽 法,所有的監(jiān)聽事件發(fā) 時(shí),都統(tǒng) 調(diào) 監(jiān)聽者的此 法!
// ?1> keyPath
// ?2> object
// ?3> change
// ?4> context
觀察的鍵值路徑
觀察的對(duì)象
改變,新、舊數(shù)值發(fā)送過來
上下 ,建 觀察時(shí)指定的上下 ,如果同時(shí)監(jiān)聽多個(gè)屬
性,可以通過上下 加以區(qū)分
-(void)observeValueForKeyPath:(NSString*)keyPath
ofObject:(id)object change:(NSDictionary
*)change context:(void*)context{
NSLog(@"keyPath = %@ , object = %@ , change = %@ ,context
= %@", keyPath, object, change, context);
}
3. RGB顏色黑色是一點(diǎn)顏色都沒有,白色包含了所有的顏色
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......4.數(shù)據(jù)存儲(chǔ)
4.1.本質(zhì)所有數(shù)據(jù)存儲(chǔ)的本質(zhì)都是做數(shù)據(jù)持久化,也就是寫入文件
4.2.存儲(chǔ)方式
NSUserDefaults存儲(chǔ)
自定義文件存儲(chǔ)Plist,txt,xml數(shù)據(jù)庫(kù)存儲(chǔ)sqlite
網(wǎng)絡(luò)存儲(chǔ)
3.應(yīng)用之間通信和分享數(shù)據(jù)的機(jī)制http://blog.csdn.net/tenfyguo/article/details/9063675
4.3.沙盒介紹
沙盒路徑是應(yīng)用程序?qū)iT為用戶存儲(chǔ)數(shù)據(jù)而存在的目錄** /Users/...../Data/...這個(gè)是沙盒路徑。
** /Users/...../Bundle/....這個(gè)是Bundle路徑。
而bundle路徑是應(yīng)用程序安裝路徑。
每個(gè)應(yīng)用程序只能訪問自己的沙盒路徑。
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......4.4.沙盒目錄結(jié)構(gòu)
4.4.1. Documents目錄:保存應(yīng)用程序自己的數(shù)據(jù)(比如:游戲進(jìn)度存檔、軟件的一些個(gè)人設(shè)置等)。通過iTunes、
iCloud備份時(shí),會(huì)備份這個(gè)目錄下的數(shù)據(jù)。
4.4.2. Tmp目錄:
存儲(chǔ)一些其他臨時(shí)數(shù)據(jù),系統(tǒng)磁盤空間不夠,手機(jī)重啟時(shí),會(huì)自動(dòng)清除這個(gè)目錄的數(shù)
據(jù)。無需程序員手動(dòng)清除該目錄中的數(shù)據(jù).iTunes、iCloud備份時(shí),不會(huì)備份這個(gè)目錄下的數(shù)
據(jù)。
4.4.3. Caches目錄:
保存從網(wǎng)絡(luò)上下載的文件(比如:聽歌時(shí)的緩存、圖片的緩存等),這個(gè)目錄下的數(shù)據(jù)
不會(huì)被自動(dòng)刪除,需要程序員自己實(shí)現(xiàn)清除目錄數(shù)據(jù)功能。iTunes、iCloud備份時(shí),不會(huì)備
份這個(gè)目錄下的數(shù)據(jù)。
4.4.4. Preference目錄:
保存通過"偏好設(shè)置"寫入的數(shù)據(jù)。iTunes、iCloud備份時(shí),會(huì)備份這個(gè)目錄下的數(shù)據(jù)。
4.5.沙盒路徑
4.5.1.獲取沙盒根目錄-拼接方式
NSString *path = NSHomeDirectory();
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......4.5.2.路徑拼接
path = [path stringByAppendingString:@”/Documents”];
path = [path stringByAppendingPathComponent:@”Documents”];
4.5.3.獲取沙盒全路徑-系統(tǒng)方式[NSSearchPathForDirectoriresInDomains() firstObject];
yes是絕對(duì)路徑no是相對(duì)路徑
NSSearchPathDomainMask
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];
NSUserDomainMask =1,//戶主 錄中NSLocalDomainMask =2,//當(dāng)前機(jī)器中NSNetworkDomainMask =4,//絡(luò)中可 的主機(jī)NSSystemDomainMask =8,//系統(tǒng) 錄,不可修改(/System)NSAllDomainsMask =0x0ffff,//全部
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......4.6.文件讀寫
4.6.1. plist文件讀寫寫入字典:
[dict writeToFile:path atomically:YES];
讀取字典:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
缺點(diǎn):
通過plist文件來保存數(shù)據(jù),無法直接將一個(gè)對(duì)象保存到文件中。
4.7.偏好讀寫
本質(zhì)就是通過plist文件存儲(chǔ)數(shù)據(jù)
但是使用起來更簡(jiǎn)單(無需關(guān)注文件、文件夾路徑和名稱)。目錄每次都會(huì)發(fā)生改變
通過"偏好設(shè)置"的 式讀、寫 件時(shí),路徑在"沙盒根 錄"->"Library"->"Preferences"
數(shù)據(jù)只能存儲(chǔ)在一個(gè)文件中,不存儲(chǔ)大批量數(shù)據(jù)(很多時(shí)候大批量數(shù)據(jù)要分多個(gè)文件存儲(chǔ))
4.7.1.寫入
NSUserDefaults*defaults = [NSUserDefaultsstandardUserDefaults];
[defaultssetObject:@"zhangsan"forKey:@"name"];
[defaultssetBool:YESforKey:@"sex"];
[defaultssetInteger:20forKey:@"age"];
[defaultssynchronize];
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......4.7.2.讀出
NSUserDefaults*defaults = [NSUserDefaultsstandardUserDefaults];NSString*name = [defaultsobjectForKey:@"name"];BOOLsex = [defaultsboolForKey:@"sex"];NSIntegerage = [defaultsintegerForKey:@"age"];
4.7.3.缺點(diǎn)
通過"plist"文件來保存數(shù)據(jù),無法直接將一個(gè)對(duì)象保存到文件中。比如Person對(duì)象就沒有
writeToFile方法。而偏好設(shè)置只能存儲(chǔ)少量數(shù)據(jù),對(duì)于大數(shù)據(jù)存儲(chǔ)不適合4.7.4.注意
使用對(duì)象:僅僅是Foundation框架中的一些類,比如:
NSString\NSArray\NSDictionary\NSSet\NSNumber\NSData,對(duì)于其它數(shù)據(jù)類型不能直接存
儲(chǔ)。
5.數(shù)據(jù)歸檔5.1.特點(diǎn)
"歸檔"是一種可以把任何對(duì)象,直接保存為文件的方式。(其中包括"歸檔"與"反歸檔(讀檔)")
5.2.前提必須是實(shí)現(xiàn)協(xié)議
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......通過"歸檔"的方式來存儲(chǔ),對(duì)象必須遵守NSCoding協(xié)議,也就意味著通過"歸檔"的方式能
將任何遵守了NSCoding協(xié)議的"對(duì)象"存儲(chǔ)到文件中
5.3.歸檔可以歸檔數(shù)組
重點(diǎn):子類必須也要讓父類歸檔。
5.4.反歸檔
重點(diǎn):父類必須init,子類必須也要調(diào)用父類的反歸檔
6.完善登錄業(yè)務(wù)6.1.偏好設(shè)置-寫入
寫入自動(dòng)登錄和記住密碼狀態(tài)
寫入用戶名密碼
//戶偏好數(shù)據(jù)寫
- (void)userDefaultWrite
{
standardUserDefaults];//2.寫 數(shù)據(jù)
BOOLisremerberPwd=self.rememberPwd.on;BOOLisAutoLogin=self.autoLogin.on;
//寫? 動(dòng)登錄和記住密碼的屬性值
[udsetBool:isremerberPwdforKey:@"isremerberPwd"];
//1.創(chuàng)建偏好寫 對(duì)象
NSUserDefaults*ud=[NSUserDefaults
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
6.2.寫入時(shí)間當(dāng)?shù)卿洺晒r(shí)寫入
6.3.自動(dòng)登錄
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......
[udsetBool:isAutoLoginforKey:@"isAutoLogin"];
//判斷是否是記住密碼,如果是就再寫? 戶名和密碼
if(isremerberPwd) {
[udsetObject:self.nameView.textforKey:@"name"];
[udsetObject:self.pwdView.textforKey:@"pwd"];
}
//3.同步
[udsynchronize];
}
[selfuserDefaultWrite];
//動(dòng)登錄功能
- (void)autoLoginAction{
//讀取偏好
NSUserDefaults*ud=[NSUserDefaultsstandardUserDefaults];
BOOLisremerberPwd=[udboolForKey:@"isremerberPwd"];BOOLisAutoLogin=[udboolForKey:@"isAutoLogin"];
//賦值開關(guān)
[self.rememberPwdsetOn:isremerberPwdanimated:YES];
[self.autoLoginsetOn:isAutoLoginanimated:YES];
//判斷是否是記住密碼如果是就再填? 戶名和密碼
if(isremerberPwd) {self.nameView.text=[udobjectForKey:@"name"];self.pwdView.text=[udobjectForKey:@"pwd"];
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......
}if(isAutoLogin) {
// [self login:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(0.5*NSEC_PER_SEC)),dispatch_get_main_queue(),
^{
[selflogin:nil];
});
}
}
6.4.注意:在主隊(duì)列啟動(dòng)登錄
if(isAutoLogin) {//這樣不能移除提 框
// [self login:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(0.5*NSEC_PER_SEC)),dispatch_get_main_queue(),
^{
[selflogin:nil];
});
}
7.私人通訊錄數(shù)據(jù)歸檔7.1.模型類
7.1.1.模型類-實(shí)現(xiàn)歸檔協(xié)議
@interfaceCZContact :NSObject
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......7.1.2.模型類-歸檔
//歸檔
- (void)encodeWithCoder:(NSCoder*)aCoder
{
[aCoderencodeObject:self.nameforKey:@"name"];
[aCoderencodeObject:self.phoneforKey:@"phone"];
}
7.1.3.模型類-反歸檔
//反歸檔
- (instancetype)initWithCoder:(NSCoder*)aDecoder
{
if(self=[superinit]) {self.name=[aDecoderdecodeObjectForKey:@"name"];self.phone=[aDecoderdecodeObjectForKey:@"phone"];
}
return self;
}
7.2.控制器7.2.1.數(shù)據(jù)歸檔
//對(duì)聯(lián)系 數(shù)據(jù)進(jìn) 歸檔存儲(chǔ)
- (void) saveContact
{
NSString
*docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentD
irectory,NSUserDomainMask,YES)lastObject];
NSString*filePath=[docPathstringByAppendingPathComponent:@"contact.data"];
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......
//歸檔操作
[NSKeyedArchiverarchiveRootObject:self.contactstoFile:filePath];
}
7.2.2.數(shù)據(jù)歸檔位置—添加聯(lián)系人
- (void) addBtnDidClick:(AddViewController*)vc
withContact:(CZContact*)contact{
//1.添加模型到數(shù)組
[self.contactsaddObject:contact];
//2.刷新表格
[self.tableViewreloadData];
//3.進(jìn) 數(shù)據(jù)--歸檔
[selfsaveContact];
}
7.2.3.數(shù)據(jù)歸檔位置—修改聯(lián)系人
- (void)editBtnDidClick:(EditViewController*)vc{//刷新tableView
// [self.tableView reloadData];
NSIndexPath*indexPath=self.tableView.indexPathForSelectedRow;
[self.tableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];
//數(shù)據(jù)存儲(chǔ)
[selfsaveContact];
}
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......7.3.數(shù)據(jù)源
源數(shù)據(jù)從歸檔文件中加載
//懶加載--從歸檔 件中獲得數(shù)據(jù)
- (NSArray*)contacts
{
if(_contacts==nil) {NSString
*docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentD
irectory,NSUserDomainMask,YES)lastObject];
NSString*filePath=[docPathstringByAppendingPathComponent:@"contact.data"];
_contacts=[NSKeyedUnarchiverunarchiveObjectWithFile:filePath];
if(_contacts==nil)//有可能為nil{
_contacts=[NSMutableArrayarray];
}
}
return_contacts;
}
8.項(xiàng)目刪除聯(lián)系人8.1.tableViewCell左滑出現(xiàn)刪除按鈕
//實(shí)現(xiàn)此 法就可以實(shí)現(xiàn)cell的左邊滑動(dòng)出刪除按鈕了
- (void)tableView:(UITableView*)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingSt
yle forRowAtIndexPath:(NSIndexPath*)indexPath{
}
//確定刪除按鈕的 本
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
8.2.刪除聯(lián)系人操作
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......
- (NSString*)tableView:(UITableView*)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSInde
xPath*)indexPath
{
return@"刪除";
}
//實(shí)現(xiàn)此 法就可以實(shí)現(xiàn)cell的左邊滑動(dòng)出刪除按鈕了//只有刪除狀態(tài)才有刪除按鈕的顯? 和 點(diǎn)擊刪除按鈕的回調(diào)操作
- (void)tableView:(UITableView*)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingSt
yle forRowAtIndexPath:(NSIndexPath*)indexPath{
//先刪除數(shù)據(jù)源
[self.contactsremoveObjectAtIndex:indexPath.row];
//重新歸檔--就是刪除歸檔 件中的已經(jīng)刪除的聯(lián)系[selfsaveContact];
//再刪除tableView中的顯
[self.tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];
}
//對(duì)聯(lián)系 數(shù)據(jù)進(jìn) 歸檔存儲(chǔ)
- (void) saveContact
{
NSString
*docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentD
irectory,NSUserDomainMask,YES)lastObject];
NSString*filePath=[docPathstringByAppendingPathComponent:@"contact.data"];
//歸檔操作--歸檔數(shù)組? 的所有對(duì)象,也就是說先刪除 件中的所有對(duì)
傳智播客致力打造專業(yè)的IT實(shí)戰(zhàn)培訓(xùn)課程——?jiǎng)?wù)實(shí)、創(chuàng)新、質(zhì)量、專注、分享、責(zé)任
想要高薪就業(yè)?還等什么?照著文檔不停的敲吧.......
象,然后重新歸檔
[NSKeyedArchiverarchiveRootObject:self.contactstoFile:filePath];
}
9. QQ主流框架9.1.目的
這個(gè)案例主要是綜合使用UITabBarController和UINavigationController.復(fù)習(xí)靜態(tài)單元格的創(chuàng)建和使用
進(jìn)一步的掌握多控制器的管理。
9.2.項(xiàng)目框架
注意:TableViewController:
Content : Static Cells
Style : Grouped
TableViewCell:
Style : Basic
Accessory : Disclosure Indicator