[BS-30] 你真的会用storyboard开发吗?

你真的会用storyboard开发吗?

 
随着苹果大屏手机的推出,苹果公司马上推出了屏幕适配的一些东西Constraints and Size Classes同时,在开发项目时候,是使用sb还是写代码,苹果公司给出了回复:使用SB开发,使用SB开发不仅省去了很多重复的代码,而且可视性非常强!但是你真的知道怎么去使用SB吗?

在一般的布局中,我们先使用一个plist文件,做为tabbarVC的的4个选项 pilist文件如下 在此输入图片描述 

然后创建多个Storyboard,以及Storyboard关联的文件, 然后删除启动的一些sb界面,让界面启动时候从AppDelegate中启动, 在AppDelegate中写下如下代码

<!-- lang: cpp -->
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor = [UIColor whiteColor]; //b不要在主方法中写碎代码 [self setUI]; [self.window makeKeyAndVisible]; return YES;

}

-(void)setUI{

UITabBarController *uiTabBar = [[UITabBarController alloc] init];

NSURL *url = [[NSBundle mainBundle] URLForResource:@"MainUI" withExtension:@"plist"];
NSArray *arr = [NSArray arrayWithContentsOfURL:url];

for (NSDictionary *dic in arr) {

    UIStoryboard *sb = [UIStoryboard storyboardWithName:dic[@"vcName"] bundle:nil];
    UIViewController *uiVC = sb.instantiateInitialViewController;
    uiVC.title = dic[@"title"];
    uiVC.tabBarItem.image = [UIImage imageNamed:dic[@"icon"]];
    uiVC.tabBarItem.badgeValue = dic[@"badgeNumber"];

    [uiTabBar addChildViewController:uiVC];
}

self.window.rootViewController = uiTabBar;

}

然后就能看到如下功能了,

效果如下:

 在此输入图片描述

那在SB文件中的按钮点击跳转到其他SB文件中怎么处理呢?如下代码:

  • (IBAction)clickBtn:(id)sender {
  • UIStoryboard *sb = [UIStoryboard storyboardWithName:@"hhh" bundle:nil];
  • UIViewController *vc = sb.instantiateInitialViewController;
  • [self.navigationController pushViewController:vc animated:YES];
  • }

注意: 通过 [XXViewController   alloc] init]; 的方法只能加载同名xib,不会加载同名的storyboard。 即使同时增加3个文件:WZTableViewController.h,WZTableViewController.m,WZTableViewController.storyboard,然后在代码中写上如下2句:

WZTableViewController *tableVC = [[WZTableViewController alloc]init];  //只会创建一个空的白色的tableView

[self.navigationController   pushViewController: tableVC  animated:YES];

也不会自动加载同名的storyboard。 加载storyboard必须采用如下代码:

  • UIStoryboard *sb = [UIStoryboard storyboardWithName:@"WZTableViewController" bundle:nil];
  • UIViewController  *settingVC = [sb  instantiateInitialViewController];

 

转自:http://my.oschina.net/panyong/blog/372385

 

 

posted @ 2016-06-18 09:11  stevenwuzheng  阅读(237)  评论(0编辑  收藏  举报