最近在學習ios,剛學的時候還是使用xib,但是后來聽另一個學習ios有一段時間的同學說,storyboard是ios5的特性,也是以后的趨勢所在吧,所以決定轉向storyboard了,之前的學習方法也有點不對,主要是在看書,有點低效率吧,聽了那個同學的建議,多看看別人的技術博客,自己也寫寫博客,把學到的知識都記錄下來,自己以后忘記了還可以回過頭溫習下。發現自己還有很多要學的,ARC,Storyboard,GCD,Block,etc.........
(轉載自:http://ryan.easymorse.com/?p=39)
Storyboard是iOS5的新特性。使用Storyboard可以更方便的管理應用的界面,同時為視圖間的跳轉提供了清晰的脈絡。
任何時候你都可以在你的項目中添加Storyboard,不管你創建項目的時候有沒有勾選“Usb Storyboard”選項,如下圖所示。
如果你創建的時候勾選了該選項,那么恭喜你,你已經有了一個Storyboard,並且被設為了Main Storyboard,如下圖所示。
同時你的代理類是這樣的:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
這跟以前可大不一樣了!這是因為程序會自動加載Main Storyboard。如果你把上面的Main Storyboard項清空了,那我們的MainStoryboard.storyboard就是一個孤立的文件了,你必須手動加載它,所以上面的代碼也得改一改,如下:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; self.window.rootViewController = [storyboard instantiateInitialViewController]; [self.window makeKeyAndVisible]; return YES; }
上面加粗的兩句比較重要,前一句是從文件創建UIStoryboard實例對象,后一句則是從這個對象里獲取“初始視圖控制器”(一個storyboard里只可以設置一個初始視圖控制器,相當於這個storyboard的入口,后面還會說到)。 但如果你創建項目的時候沒有勾選“Use Storyboard”項,那你就要手動添加Storyboard了,如下圖所示。
手動創建的Storyboard和自動創建的沒有任何不同,打開我們剛剛創建的Storyboard,里面什么都沒有,我們不妨拖拽一個Navigation Controller到里面。
可以看到我們一次拖出了兩個視圖控制器,這是因為Navigation Controller需要一個rootViewController。注意我標注的兩個小箭頭,左邊的代表這是一個“初始視圖控制器”,右邊的代表兩個視圖控制器之間的關系。選中視圖控制器可查看屬性。
取消勾選“Is Initial View Controller”后指向視圖控制器的箭頭也就消失了。再次說一下,一個Storyboard里只能有一個“初始視圖控制器”。
下面要做的是在根控制器中添加一個button,然后繼續拖拽一個ViewController,點擊button新ViewController push進來。
首先添加button,就跟使用IB一樣。這次我們拖拽一個UITabbarController,這時如下圖所示。
接下來有兩種方法: 第一種:我們可以直接在Storyboard中完成,按住ctrl從button連接到TabbarController,松開並選擇push,這樣我們就創建了一個segue(UISotryboardSegue)。
保存並運行
我們沒有寫任何代碼就已經完成了一個簡單的框架。
下面是第二種方法:我們准備用代碼來實現。(記得把上一步中push類型的segue刪除。)首先創建類FirstViewController(Subclass of UIViewController),然后將RootViewController的底層類改為FirstViewController,如下圖。
繼續選中TabbarController,並設置Identifier為“second”。
在FirstViewController.m中添加下面的方法並連接到button。
- (IBAction)pressed { UIStoryboard *board=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UITabBarController *nextViewController =[board instantiateViewControllerWithIdentifier:@"second"]; [self.navigationController pushViewController:nextViewController animated:YES]; }
加粗的代碼展示了如何使用Identifier從Storyboard中獲取指定的視圖控制器。保存並運行,效果和前一種方法完全相同。
下面我們將完成另一個目標:手動觸發一個segue。(Storyboard里無法創建一個通過touch來觸發的segue。)
拖拽一個UIViewController,按住ctrl從TabbarController的第一個分支連接到新的viewController,同樣選擇push。
選擇剛剛創建的segue並設置Identifier為“push”。(Identifier只是一個名稱沒有實際意義,可以隨便設置。)
添加新類SecondViewController(Subclass of UIViewController),選中第一個分支並設置其底層類為SecondViewController。然后添加如下方法。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self performSegueWithIdentifier:@"push" sender:nil]; }
明白這個方法后就可以用任何類型的事件去觸發我們在Storyboard中創建的segue了。
繼續下一個目標:在segue被觸發的時候傳遞參數到目標視圖控制器。
刪除TabbarController的第二個分支,拖拽一個UITableViewController,按住ctrl從TabbarController連接到UITableViewController,松開並選擇relationship。選中cell設置Identifier為“cell”。然后再拖拽一個UIViewController,並添加一個UILabel。作如下操作。
添加類ThirdViewController(Subclass of UITableViewController)和類FourthViewController(Subclass of UIViewController),分別設置為最后兩個視圖控制器的底層類。
//ThirdViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 20; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @”cell“; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row]; return cell; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([sender isKindOfClass:[UITableViewCell class]]) { if ([segue.destinationViewController isKindOfClass:[ForthViewController class]]) { NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; FifthViewController *nextViewController = segue.destinationViewController; nextViewController.string = [NSString stringWithFormat:@"%d",indexPath.row]; } } } //ForthViewController.h @interface ForthViewController : UIViewController { IBOutlet UILabel *label; } @property (nonatomic,strong) NSString *string; //ForthViewController.m @synthesize string; - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; label.text = self.string; }
segue包含屬性sourceViewController和destinationViewController。segue在觸發后但還未執行的時候會調用sourceViewController的prepareForSegue:sender:方法,sender是segue的起始點,可以是button、cell等等。這是你傳遞參數最好的時機,你可以利用sender參數來找到是哪個cell被點擊,利用segue參數來獲取源視圖控制器對象和目標視圖控制器對象,這樣你就有足夠的信息來傳遞參數了。
保存並運行
源碼下載:StoryBoardDemo.rar