SB加載到內(nèi)存
創(chuàng)建home.storyboard文件碧查、然后通過代碼加載到內(nèi)存中耗帕。
//初始化initialViewController (即Storyboard entry point的箭頭指向的VC)
UINavigationController *homeVC = [[UIStoryboard storyboardWithName:@"home" bundle:nil] instantiateInitialViewController];
//加載home.storyboard里面指定Identifier的VC、(需要在SB文件中指定該VC的Identifier值)
HomeSearchResultViewController *searchVC = [[UIStoryboard storyboardWithName:@"home" bundle:nil] instantiateViewControllerWithIdentifier:@"HomeSearchResultVC"];
searchVC.keyword = @"sample";
[self.viewController.navigationController pushViewController:searchVC animated:YES];
SB里的VC跳轉(zhuǎn)
VC中間的跳轉(zhuǎn)可以使用segue來實現(xiàn)的兴垦。(需要在SB文件里去通過拖拽拉線連接VC之間的Segue字柠、并指定Segue的id,這樣才能正確的訪問到SB中眾多的Segue中的唯一一個)
使用segue轉(zhuǎn)場時窑业,會響應(yīng)-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
方法。其中segue就是storyBoard轉(zhuǎn)場控制對象鲤氢,在參數(shù)segue中能夠獲取所要跳轉(zhuǎn)的視圖控制器西潘,destinationViewController(目標(biāo)vc),sourceViewController(源視圖vc)喷市。
一、不需要傳遞參數(shù)到目標(biāo)VC的情況:
[self performSegueWithIdentifier:@"toLoginVC" sender:nil];
二品姓、需要傳遞參數(shù)的情況:
//先調(diào)用performSegueWithIdentifier方法,然后自動觸發(fā)prepareForSegue方法
[self performSegueWithIdentifier:@"toLoginVC" sender:nil];
//在當(dāng)前的sourceViewController里面添加下面的prepareForSegue方法衬潦,在VC跳轉(zhuǎn)前作參數(shù)傳遞的工作
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"toLoginVC"]) {
LoginViewController *loginVC = (LoginViewController *)segue.destinationViewController;
loginVC.account = @"xxxxx";
}
}
上面這種方式有一個痛點植酥,就是跳轉(zhuǎn)要有兩處的代碼,首尾分離,不便管理哎媚。
現(xiàn)在考慮把本來在prepareForSegue里面的代碼和performSegueWithIdentifier方法聚合到一起,方便管理稻据。
就有了下面這種方式去實現(xiàn):
KBSegueSender *sender = [[KBSegueSender alloc] initWithSend:self prepareBlock:^(UIStoryboardSegue *segue) {
BindPhoneViewController *bindPhoneVC = (BindPhoneViewController *)segue.destinationViewController;
bindPhoneVC.wx_user_id = wxId;
}];
[self performSegueWithIdentifier:@"toBindPhoneVC" sender:sender];
KBSegueSender是定義的Model买喧,用來包裹block的屬性。
#import <Foundation/Foundation.h>
typedef void(^PrepareBlock)(UIStoryboardSegue * segue) ;
@interface KBSegueSender : NSObject
@property(assign)id sender;
@property(assign) PrepareBlock prepareBlock;
- (id)initWithSend:(id)sender prepareBlock:(PrepareBlock) block;
@end
#import "KBSegueSender.h"
@implementation KBSegueSender
- (id)initWithSend:(id)sender prepareBlock:(PrepareBlock) block {
self = [super init];
if (self) {
self.sender = sender;
self.prepareBlock = block;
}
return self;
}
@end
//通過Block將參數(shù)傳遞代碼聚合起來淤毛,然后通過創(chuàng)建Category來在prepareForSegue去執(zhí)行block,完成值得傳遞姓言。
#import "UIViewController+KBAdditional.h"
#import "KBSegueSender.h"
@implementation UIViewController (KBAdditional)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([sender isKindOfClass:[KBSegueSender class]]) {
KBSegueSender *segueSender = (KBSegueSender*)sender;
segueSender.prepareBlock(segue);
}
}
@end