UISplitViewController可以用來干嘛拷沸?簡言之,能讓屏幕呈現(xiàn)master-detail風(fēng)格椭符。
詳細(xì)說來,它將屏幕分成兩個(gè)section耻姥,在左邊這個(gè)section顯示master view controller销钝,右邊這個(gè)section顯示detail view controller。但在iPhone上有所不同:首先顯示的是detail view controller琐簇,選擇navigation bar 上的back button蒸健,可以返回到master view controller。
項(xiàng)目示意圖:
01
--新建兩個(gè)文件婉商,命名為MasterViewController和DetailViewController似忧。
--在storyboard里,拖一個(gè)SplitViewController出來丈秩,將Root View Controller的class設(shè)置為MasterViewController盯捌,將下面那個(gè)ViewController的class設(shè)置為DetailViewController。
02
Data Model的搭建蘑秽。新建文件饺著,命名為Hearts。
03
MasterViewController的搭建肠牲。
--創(chuàng)建一個(gè)數(shù)組幼衰,并對它初始化。
--添加三個(gè)數(shù)據(jù)源方法缀雳。
04
DetailViewController的搭建渡嚣。
05
建立MasterViewController和DetailViewController之間的聯(lián)系。
--在MasterViewController.swift里添加代理和協(xié)議和相應(yīng)的方法俏险。
這里的delegate是啥严拒?遵守協(xié)議的變量。我的理解是竖独,delegate像傳接球的人一樣裤唠,把從MasterViewController獲得的變量接住,再傳給DetailViewController莹痢。
在Master-Detail風(fēng)格的Template中种蘸,MasterViewController有一個(gè)對DetailViewController的引用,這意味著選中當(dāng)row被選中時(shí)竞膳,MasterViewController能將屬性set在DetailViewController上航瞭。接下來,添加能實(shí)現(xiàn)這個(gè)的方法坦辟。這里實(shí)現(xiàn)了兩步刊侯,第一步是delegate接到從MasterViewController傳過來的變量,第二步是View Controller的轉(zhuǎn)換锉走。
接下來要delegate要做的就是滨彻,把接到的變量傳給DetailViewController藕届。
--在DetailViewController.swift里添加代碼。當(dāng)row被選中時(shí)亭饵,DetailViewController的property能隨之更新休偶。
--最后在AppDelegate.swift里的application(_:UIApplication,didFinishLaunchingOptions launchOptions:[NSObject:AnyObject]?)方法里添加以下代碼。
項(xiàng)目完成辜羊。
Github項(xiàng)目地址: