一、storyBoard的使用
StoryBoard的本質(zhì)是一個xml文件,在編譯時生成nib的二進(jìn)制文件,運(yùn)行時nib文件被加載并開始創(chuàng)建和實(shí)例化GUI元素
使用storyBoard的項(xiàng)目均以初始化storyBoard文件作為整個程序的初始化入口
xib和StoryBoard的對比
相同:都屬于IB編程的方式,可以快速創(chuàng)建GUI
不同:xib側(cè)重于單文件編輯,storyBoard側(cè)重于多頁面關(guān)聯(lián)。storyBoard可以直觀看到頁面之間的邏輯,并且所有頁面跳轉(zhuǎn)邏輯均可在
-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender方法中完成方便界面間數(shù)據(jù)統(tǒng)一管理。
這里還有一個問題,如果界面過多,可以放在不同的storyBoard里,邏輯更加清晰,渲染也更快。根據(jù)新的storyBoard的名字,就能通過代碼得到對應(yīng)的storyBoard。
1.設(shè)置主啟動的storyBoard
這兩個地方填storyBoard的名字,決定哪個storyBoard是入口,兩處修改一處即可,編譯時另一處會自動修改
第一圖是工程的General中
2.設(shè)置應(yīng)用程序的初始窗體,設(shè)置該storyBoard的第一個視圖是哪個ViewController
3.在storyBoard中的Controller可以設(shè)標(biāo)識,通過標(biāo)識在storyBoard中創(chuàng)建視圖控制器對象
//得到storyBoard
UIStoryboard *tabBarSB = [UIStoryboard storyboardWithName:@"TabBarController" bundle:nil];
//得到VC
UITabBarController *tabBarC = [tabBarSB instantiateViewControllerWithIdentifier:@"tabBarC"];
二、storyBoard的跳轉(zhuǎn)和segue
storyBoard頁面跳轉(zhuǎn)有兩種
1.代碼方法使用代碼通過segue標(biāo)識來跳轉(zhuǎn),segue要提前定義好,要把identifier填好
如果想進(jìn)行判斷后再跳轉(zhuǎn),比如登錄驗(yàn)證,就可以使用下面的方法,但是拉segue的時候就不從button上面拉,選中第一個ViewController,拉到第二個ViewController可以產(chǎn)生一個view到view的segue,通過按鈕觸發(fā)這個segue
通過點(diǎn)擊按鈕調(diào)用下面的方法來觸發(fā)這個segue
[self performSegueWithIdentifier:@"toSecondSegue" sender:nil];
2.直接拖拽,拖拽按鈕的連線關(guān)聯(lián)兩個頁面,會產(chǎn)生一條關(guān)聯(lián)線segue,不需要添加相應(yīng)方法
show:push出下個界面
show Detail:replace
Present Modally:模態(tài)出下個界面
Present As Popover:模態(tài)推出
Custom:自定義,需要自定義segue
自定義segue
1.新建一個類繼承于UIStoryBoardSegue
2.選中前一個視圖控制器,連線后選中Custom
3.選中自定義segue,設(shè)置segue的identifier和關(guān)聯(lián)類
4.在segue里重寫perform方法(界面間跳轉(zhuǎn)默認(rèn)執(zhí)行的方法),自定義跳轉(zhuǎn)效果
-(void)perform
{
//獲取當(dāng)前VC
UIViewController *vc_1 = [self sourceViewController];
//目標(biāo)
UIViewController *vc_2 = [self destinationViewController];
//跳轉(zhuǎn)
[vc_1.navigationController pushViewController:vc_2 animated:YES];
//這里提一下使用加動畫的跳轉(zhuǎn)
//如果是兩個ViewController之間需要動畫跳轉(zhuǎn),需要將將要出現(xiàn)的VC作為當(dāng)前VC的子控制器
//否則進(jìn)行下面的頁面切換,新的view上的控件都是不顯示的
//這樣大家共用一個navigationController
[vc_1.navigationController addChildViewController:vc_2];
[vc_1.navigationController.view addSubview:vc_2.view];
//自定義頁面切換效果,新的界面的控件不顯示
[UIView transitionFromView:vc_1.view toView:vc_2.view duration:0.5
options:UIViewAnimationOptionTransitionFlipFromTop completion:nil];
//如果要各自使用不同的navigationController
/*
UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:vc_2];
[self.navigationController addChildViewController:navC];
[self.navigationController.view addSubview:navC.view];
[UIView transitionFromView:self.navigationController.view toView:navC.view duration:0.25
options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
*/
}
界面間傳值
在跳轉(zhuǎn)時會執(zhí)行-(void)prepareForSegue:(UIStoryBoardSegue*)segue sender:(id)sender方法
在這個方法里根據(jù)不同的segue標(biāo)識符來完成數(shù)據(jù)處理
//segue:轉(zhuǎn)場,通過該方法以得到(A push B) A和B
//sender:我們點(diǎn)擊某個控件觸發(fā)的跳轉(zhuǎn)動作
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//這里有個問題,如果控制器有多個segue,要進(jìn)行判斷,不然后續(xù)對錯誤的控制器進(jìn)行操作,會崩
//判斷是哪個segue
if ([segue.identifier isEqualToString:@"toSecondSegue"]) {
NSLog(@"to detailVC");
//得到發(fā)起跳轉(zhuǎn)的ViewController
UIViewController *selfController = [segue sourceViewController];
//得到目標(biāo)ViewController,即將跳轉(zhuǎn)到的ViewController
SecondViewController *secondVC = [segue destinationViewController];
secondVC.myTitle = @"1111";
}
//還有一種通過storyBoard得到VC的方法,要給視圖控制器設(shè)置好storyBoardID
// UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
// SecondViewController *secondVC = [mainSB instantiateViewControllerWithIdentifier:@"SecondVC"];
//但這種方法賦值不了,下面一句沒有效果
// secondVC.myTitle = @"2222";
}
三、storyBoard自定義cell
storyBoard自定義cell的步驟,這個不記少了什么就很煩
如果我們自己拖一個UITableViewController,或者,給一個ViewController上拖一個UITableView,再給UITableView上拖一個UITableViewCell
我們會看到這樣的情況,當(dāng)然,上面的label和button要自己加
以UITableViewController為例
1.先拖一個UITableViewController控件到storyBoard
2.創(chuàng)建一個UITableViewController類
3.將兩者關(guān)聯(lián)
4.創(chuàng)建一個UITableViewCell的類,并讓它于控件上的CELL關(guān)聯(lián),這里要注意,一定要點(diǎn)對位置,確定選擇了cell
這一看那個拖拉大小的小點(diǎn),選擇了cell一定會出現(xiàn)
5.在storyBoard上給cell添加控件,并與自定義的CustomTableviewCell類關(guān)聯(lián)。
6.在storyBoard創(chuàng)建表視圖的方法中,在UITableViewController類中不需要進(jìn)行cell的注冊,導(dǎo)入CustomTableviewCell,直接使用即可
如果使用系統(tǒng)自帶的可視化板上的Cell,也是要設(shè)置Identifier的,不用注冊。
7.其他代理方法與Tableview無異