iOS開發中,蘋果公司提供了一種可視化的編程方式:即xib和storyboard,xib相對來說比較靈活,可以在純代碼的項目中使用,
也可以和storyboard配合使用,用法都差不多,下面來總結一下故事版的使用方法:
1,初始
選中viewcontrolller,在屬性面板里勾選Is Initial View Controller,即可設置為其實場景,前面會有灰色的小箭頭
2,將viewconroller的尺寸改成iPhone的大小
3,添加segue
使用segue的好處是,頁面的千幻不在需要創建任何代碼,按住ctrl鍵同事Tod's空間到目標場景,在彈出的上下文菜單中選擇show
1)菜單中間4個是過去版本的使用方式,推薦最上面的4個新的方式:
Show:就是Push一個新的視圖
Show Detail:替換當前的視圖方式來展現新的視圖
Modally:模式窗口的方式
Popover:浮窗形式
2)如果兩個Controller之間建立包含關聯,例如從TableBarController到NavigationController,則上下文菜單會有relationship的選項,選擇viewcontroller即可。
4,給segue添加關聯類
在storyboard中添加一個segue時並不會同步添加對應的類,如果需要,得手動添加!
5,添加關聯代碼
主要有兩種關聯類型,一種是outlet連接,就是在代碼里面創建界面元素的成員變量引用,另一種是Action時間,把界面元素的相應時間方法添加到代碼里來。
6,同一個storyboard里多個View Controller的引用
var rootViewController = UIStoryboard(name: "Main", bundle: nil) .instantiateViewControllerWithIdentifier("RootView") as UIViewController
對於初始Viewcontroller也可以不通過identity直接換區:
var rootViewController = UIStoryboard(name: "Main", bundle: nil) .instantiateInitialViewController() as UIViewController
7,使用多個storyboard文件
一個項目可以不止一個storyboard文件,他們之間也可以互相調用,假如還添加一個XXX.storyboard,里面的viewcontroller設置identity為secondView,則我們可以通過導航的方式來關聯兩個storyboard文件。
在AppDelegate的Application入口里把Main面板放入導航控制:
var rootViewController = UIStoryboard(name: "Main", bundle: nil) .instantiateInitialViewController() as UIViewController self.window!.rootViewController = UINavigationController(rootViewController: rootViewController)
然后可以在RootView里放入一個安潤,點擊事件里導航到想要去的頁面:
var viewController = UIStoryboard(name: "Second", bundle: nil) .instantiateViewControllerWithIdentifier("SecondView") as UIViewController self.navigationController?.pushViewController(viewController, animated: true)