由于進入新的公司接觸新的項目基本使用的是storyBoard開發(fā),之前沒怎么用過就借著項目學(xué)習(xí)了一下,下面是我學(xué)習(xí)別人總結(jié)結(jié)合自己認識得到的基礎(chǔ)知識結(jié)構(gòu)

基礎(chǔ)知識
優(yōu)勢
- storyboard能將nib匯總,便于管理
- storyboard可以通過segue完成各種場景的過度
- 支持tableview的prototype cell,可以在storyboard中編輯cell
在 storyboard 中使用自定義的控制器和 view
-
使用自定義的控制器
1 首先需要創(chuàng)建一個viewcontroller的子類,如我們常用的tabBarController
2 我們需要拖一個view controller 到main.storyBoard中,然后修改它的class為你創(chuàng)建的子類
image.png
3.此時你可以在這個controller中添加你的控件、關(guān)聯(lián)控件
- 使用自定義的 view
這個和使用自定義控制器步驟是一樣的,和之前 用xib 自定義控件也差不多,只不過不需要我們另外創(chuàng)建一個 xib 文件了,直接在 controller 中創(chuàng)建,就不多說了
- cell
cell 在 storyboard 創(chuàng)建和上面的 view 創(chuàng)建是一樣的,需要注意的是 cell 有兩種prototype cells 和
static cells
1.prototype cells
選中tableview,設(shè)置tableview的 cotent 為 Dynamic Propotype

這中種cell 是可以重用的,我們可以設(shè)置的 identityId 來區(qū)分不同的 cell,但是不能直接把cell中的view連線到cell所屬的viewController中,因為這個是可以重用的 cell 所以無法確定連接的是哪一個 subview
2.static cells
使用靜態(tài)的cell,適用在僅有幾個確定cell的tableview中,不能重用,設(shè)置了幾個cell,就顯示幾個cell,static cell 的cell以及cell中的控件都相當于確定的view,因此,static cell可以把cell以及cell中的控件連線到cell所屬的viewController中。
加載 storyBoard
在一個項目中使用 storyboard肯定是需要一個入口加載它
- 獲取 storyboard 文件
- 作為程序主入口
我們剛創(chuàng)建的項目中一般都會有一個main.storyBoard,如果此時運行就會加載這個 storyboard 作為 window 的 rootviewcontroller
我們項目中的info.plist中填寫默認加載的是main.storyBoard文件 ,當然你也可以在此處修改你最先加載的入口為 你自己創(chuàng)建的 storyboard 文件
image.png - 通過代碼加載
以上是不通過代碼來加載 storyboard作為程序的主入口 ,我們也可以通過代碼來找到我們需要的 storyboard ,在我們需要的地方去使用它
UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"`你的storyBoard名稱`" bundle:nil];
獲取控制器
1.初始化storyBoard的主入口controller
storyboard文件需要一個初始化controller來作為他的主入口,這個和 window 需要一個 rootviewcontroller 差不多
-
在 storyboard 中選一個controller作為主入口,勾選Is Initial View Controller
image.png - 通過代碼來初始化
UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"`你的storyBoard名稱`" bundle:nil];
//下面這個方法代表著創(chuàng)建storyboard中箭頭指向的控制器(初始控制器)
MYViewController *controller=[storyboard instantiateInitialViewController];
//參考
// UINib *nib=[UINib nibWithNibName:@"`你的storyBoard名稱`" bundle:nil];
// [nib instantiateWithOwner:nil options:nil];
2.初始化任意 storyboard 中的 controller
有時候我們需要在一個 storyboard 文件中去加載其他 storyboard 文件中的控制器,那么我們需要先找到這個storyboard 文件,再通過 storyboardID 來找到相應(yīng)的控制器,類似于 cell 的identityID

UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"你的storyBoard名稱" bundle:nil];
//創(chuàng)建指定的控制器
UIViewController *controller=[storyboard instantiateViewControllerWithIdentifier:@"MYTabViewControllerID
創(chuàng)建 container 控制器和它的自控制器的 segue
對于container view controller,即Tab Bar Controller,Navigation Controller 可以通過拖拽創(chuàng)建設(shè)置relationship segue。
Tab Bar Controller 需要設(shè)置 viewcontrollers 可以通過右擊鏈接到Navigation Controller ,Navigation Controller 也可以通過這種方式來設(shè)置 rootviewcontroller
至此我們至少可以搭建一個大概的 UI 框架了 如圖

當然我們需要在 storyboard 中修改 tabbar 的控制器名稱,這個需要選中對應(yīng)的navgationcontroller 中的tabbarItem 中設(shè)置

segue 的使用
什么是 segue
在storyBoard上每一根用來界面跳轉(zhuǎn)的線,都是一個UIStoryBoardSegue對象.(簡稱Segue)
UIStoryBoardSegue的屬性
每一個Segue對象,都有3個屬性
給Segue設(shè)置唯一標識:
@property (nonatomic, readonly) NSString *identifier;
來源控制器
@property(nonatomic, readonly) id sourceViewController;
目標控制器
@property(nonatomic, readonly) id destinationViewController;
segue 的類型
- iOS8之前:
在storyboard中,segue有幾種不同的類型,在iphone和ipad的開發(fā)中,segue的類型是不同的。
在iphone中,segue有:push,modal,和custom三種不同的類型,這些類型的區(qū)別在與新頁面出現(xiàn)的方式。
而在ipad中,有push,modal,popover,replace和custom五種不同的類型。
modal: present 一個控制器或者 view
popover(iPad only):采用浮動窗的形式把新頁面展示出來
replace (iPad only):替換當前scene,
custom:
- iOS8之后
show: 在iPhone 中類似與 push 在 ipad 中要區(qū)分只顯示 master 或者 detail 還是兩者都有,只顯示一種的話就會和 push 一樣把新的控制器壓倒棧頂,兩者都有的話會將內(nèi)容將會壓入detail區(qū)域
present modally:模態(tài)展示內(nèi)容屬性面板中提供presentation style (UIModalPresentationStyle)與 transition style (UIModalTransitionStyle)兩種選項
popover presentation(好像iPad only):在當前的view上出現(xiàn)一個小窗口來展示內(nèi)容,
在某個現(xiàn)有視圖中的錨點處使用彈出框展示內(nèi)容。這個選項可指定顯示在彈出框視圖一邊上的箭頭可用方向,同時也是指定錨點視圖的一個選項。
custom:自定義
跳轉(zhuǎn)執(zhí)行過程
segue 跳轉(zhuǎn)的觸發(fā)方式可以分為兩種
-
自動觸發(fā):點擊某個控件后(某個按鈕),自動執(zhí)行Segue,自動完成界面的跳轉(zhuǎn)
選中按鈕通過右鍵后選擇 action 鏈接目標控制器選擇彈出方式
手動觸發(fā): 需要通過寫代碼手動執(zhí)行Segue,才能完成界面的跳轉(zhuǎn)
選擇控制器右鍵選擇 triggered segue 鏈接目標控制器選擇跳出方式

另外還需要給這個 segue 設(shè)置一個標簽

最后在該跳轉(zhuǎn)的時候,使用performSegueWithIdentifier方法執(zhí)行對應(yīng)的Segue
[self performSegueWithIdentifier:@"push" sender:nil];
segue 跳轉(zhuǎn)的執(zhí)行過程
- 手動調(diào)用performSegueWithIdentifier:sender:方法實現(xiàn)跳轉(zhuǎn)。那么這期間發(fā)生了什么呢?大致分為三個部分。
1.根據(jù)identifier去storyboard中找到對應(yīng)的線,新建UIStoryboardSegue對象
- (instancetype)initWithIdentifier:(NSString *)identifier source:(UIViewController *)source destination:(UIViewController *)destination; // Designated initializer
2.調(diào)用sourceViewController的下面方法,做一些跳轉(zhuǎn)前的準備工作并且傳入創(chuàng)建好的Segue對象
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;
所謂跳轉(zhuǎn)前的準備,因為可以拿到Segue(來源控制器,目標控制器),所以就可以在這里給下一個控制器傳遞數(shù)據(jù)。這個方法是系統(tǒng)默認調(diào)用,所以只需要實現(xiàn)即可。另外,只能由來源控制器調(diào)用,來拿到目標控制器。
3.調(diào)用Segue對象的perform方法開始執(zhí)行界面跳轉(zhuǎn)操作。
[segue perform]
- [segue perform]方法的底層實現(xiàn);
調(diào)用Segue對象的- (void)perform;方法開始執(zhí)行界面跳轉(zhuǎn)操作
1.如果segue的style是push取得sourceViewController所在的UINavigationController,調(diào)用UINavigationController的push方法將destinationViewController壓入棧中,完成跳轉(zhuǎn)
2.如果segue的style是modal,調(diào)用sourceViewController的presentViewController方法將destinationViewController展示出來
跳轉(zhuǎn)
segue可以實現(xiàn)頁面間跳轉(zhuǎn),除了上面的 relationship segue 還有 Action segue 和 Manual segue,分別對應(yīng)button跳轉(zhuǎn)和viewController跳轉(zhuǎn)。
跳進
Action segue 比較簡單,就是將button連到要展示的viewController上,當點擊時,就會觸發(fā)。
Manual segue 相對比較麻煩,但是比較靈活。它設(shè)置了兩個viewController的跳轉(zhuǎn)關(guān)系,在你需要的時候出發(fā)跳轉(zhuǎn)。



