星期四, 5月 29, 2014

[iOS] 導覽列與Storyboard

經過第一個範例([iOS] 快快樂樂寫iOS hello world (storyboards))練習後,
接著要開始多個頁面的切換練習,這次要練習透過storyboard切換不同的ViewContoller。

版本

XCode 5 


開始實作


Step1: 新增第二個ViewController

在第一個ViewController放入一個按鈕(取Load from storyboard),
然後在Object Library拖進來第二個ViewController


Step2:  Storyboard新增第二個控制器的ViewController

1) File->New->File


2) 建立一個SBViewController


3) 建立資料夾來分類控制器 (SBView) => 看個人喜好


4) 建立場景的Action Seque

點選按鈕(需按住Control鍵)拖拉到SBViewController,選擇Action Seque的push (轉換scene場景的效果)

之後就會看到Seque連結起來了

5) 將SBViewController與 storyboard的ViewController做連結 (每個視圖都有一個ViewController)

先點選storyboard的第二個ViewController,然後設定他的Custom Class為SBViewController。

如果現在按執行的話,就會發生錯誤,因為忘了加Nevigation Controller



6) 加入Navigation Controller

從Object library拖拉一個Navigation Controller,接著將預設的Root View Controller砍掉,建立Navigation Controller到原本的Root View

 一樣做拖拉,之後選擇Relationship Seque的root view
接著點選Navigation Controller並把Initial Scene打開即可(等同於拉那第一個的箭頭)


完成後如下所示,接著就能跑啦
7) 接著第SBViewController加個Label表示這頁是從storyboard載入過來的:D



SBViewController.h
#import 

@interface SBViewController : UIViewController{

    IBOutlet UILabel *sbview_lbl;
}


@end

SBViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    sbview_lbl.text = @"From storyboard content";
}

記得要把storyboard上的Label與sbview建立連結

8) 看看成果

點擊按鈕後,會看到第二個SBViewController,上面的< Back就是透過NavigationController產生的,不用寫Code


 => 


後紀


發現先前使用SBViewController這個命名方式會導致ViewController Scene會怪怪的,原來連續二個大寫字母會導致解析名稱錯誤XD





沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails