storyboard是在ios5中引入的新控件,可以更加清晰、簡單的整合多個ViewController的關係,下面主要介紹一下怎麼初建一個storyboard的工程。有關storyboard的介紹在後面的文章中提到。ios

  1. 首先利用xcode4.2建立一個新項目,選擇空工程:

image

2.填寫項目名稱和勾選使用ARCxcode

image

 

三、註釋掉AppDelegate中的如下代碼。app

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

//    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
//    // Override point for customization after application launch. 
//    self.window.backgroundColor = [UIColor whiteColor]; 
//    [self.window makeKeyAndVisible]; 
    return YES; 
}框架

四、添加storyboard控件,起名叫MainStoryBoardide

image

五、添加MainStoryBoard到工程中post

image

6.在MainStoryBoard中添加viewController控件spa

image

七、編譯運行日誌

image

 

 

下面以一個實際項目來,分上面三步來詳細介紹一下storyboard的使用code

第一步:建立UITabBarViewController、UINavgationController、UIViewController共同使用事件

最後實現的storyboard的效果是:

image

 

介紹一下上面的結構,其中tabBarController是storyboard的開始視圖,見下圖:

image

由tabBarController分出三個子視圖,其中前兩個子視圖是navigationcontroller,一個是viecontroller。其中navigationcontroller中有相應的子viewcontroller。

下面開始實現上面的工程:

1)建立一個工程叫stroyboarddemo,選擇空工程

image

建立好以後的截圖:

image

2)添加一個storyboard

image

起名爲:Storyboard,打開初建立的storyboard,能夠看到什麼也沒有。

image

3)添加一個tabBarController

image

image

image

4)把storyboard添加到工程中。在NationalLibraryConteollerAppDelegate中添加下面的代碼。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    // Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 
    
    
    UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:@"Storyboard" bundle:nil]; 
    self.window.rootViewController=[stryBoard instantiateInitialViewController]; 
    
    return YES; 
}

首先是獲取到storyboard的引用,而後是找到跟controller。

5)編譯運行代碼。

image

6)下面把tabBarController的子視圖換成navigationcontroller

打開soryboard,刪除tabBarController的孩子。

image

而後添加兩個navigationcontroller

image

navigationcontroller和tabBarController關聯起來。

按住control建拖動鼠標,選擇關聯便可。

image

image

下面修改兩個navigationcontroller跟controller的view的顏色。

image

7)在次編譯運行。

imageimage

8)在navigationcontroller中在添加孩子視圖

我先添加了三個viewcontroller

image

而後他們分別與控制器相連,相連以前先在相應的父視圖中添加一個下一頁的按鈕。

image

下面按鈕和新添加的viewcontroller相聯繫

image

一樣在添加其餘的按鈕。

image

9)運行

imageimage

imageimageimage

目前爲止,咱們尚未添加一行代碼,便可實現一個相對比較複雜的控制器。雖然實現了一個比較複雜的控制器的框架,可是在實際項目中,每一個視圖中的數據多是動態加載的,這時候咱們須要和代碼相關聯。接下來我將介紹一下storyboard怎麼和相應的代碼相關聯。

10)、添加相應viewcontroller的實體類FirstViewController

image

11)、把FirstViewController和相應的視圖相關聯

image

12)在FirstViewController中添加按鈕的事件。

-(IBAction)onClickButton:(id)sender 

    NSLog(@"FirstViewController on click button"); 
}

而且和相應的按鈕相關聯。

13)運行,當點擊綠色視圖中的下一頁的時候,出現日誌:

image

由於按鈕有兩個事件,一個是執行上述方法,還有一個做用是壓棧下一個視圖進入控制器。其前後順序是先執行方法在壓棧。

第二步、xib和storyboard共同使用

咱們常須要實現如下需求,首先是一個登陸頁面或者是註冊頁面。登陸成功以後跳入到上面複雜的導航界面。下面詳細介紹一下怎麼實現一個xib實現的登陸頁面,跳轉到storyboard的導航頁面。

1)先建立一個登陸頁面LoginViewController

image

2)修改NationalLibraryConteollerAppDelegate,先進入登陸頁面。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    // Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 
    
    LoginViewController *loginViewController=[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil]; 
//    UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:@"Storyboard" bundle:nil]; 
    self.window.rootViewController=loginViewController;//[stryBoard instantiateInitialViewController]; 
    
    return YES; 
}

3)運行便可。

image

4)在xib中,添加登陸按鈕。

image

5)添加登陸按鈕相應的事件

-(IBAction)onClickButton:(id)sender 

    
}

6)xib和按鈕事件相關聯

image

7)按鈕事件和上面的複雜控制器相關聯

-(IBAction)onClickButton:(id)sender 

    UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:@"Storyboard" bundle:nil]; 
    self.view.window.rootViewController=[stryBoard instantiateInitialViewController]; 
}

8)運行

imageimage

第三步是多個storyboard共同使用

多人合做或者項目有必定的複雜度,使用一個storyboard,確定使storyboard複雜,咱們能夠根據需求把複雜的邏輯拆分紅若干個storyboard。

1)咱們在上面的基礎上在添加一個tabBarController的孩子

image

2)添加一個ManagerViewController

image

3)、新添加的ManagerViewController和相應的控制器相關聯

image

4)在視圖中添加按鈕

image

5)在ManagerViewController中添加按鈕事件

-(IBAction)onClickButton:(id)sender 
{

}

6)按鈕事件和視圖按鈕相關聯

image

7)添加一個新的storyboard,叫作second

image

8)添加導航器

image

9)在ManagerViewController的按鈕事件中導航進入second便可

-(IBAction)onClickButton:(id)sender 

    UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:@"second" bundle:nil]; 
    [self presentModalViewController:[stryBoard instantiateInitialViewController] animated:YES]; 
}

10)運行

imageimageimageimageimage

 

上面就是使用storyboard實現一個相對比較複雜的項目。