前言:在实际来发中,作者一般都是使用纯代码的方式进行开发,国内很多开发者都喜欢使用纯代码进行开发,一方面是代码可维护性比较高,另一方面也是因为StoryBoard团队协作的诟病。不过如果把一个项目拆分为多个StroyBoard,不同的人负责不同的模块,那就能很好的解决一些问题。本文主要讲解如何把一个项目拆分在多个StoryBoard上。

正确的命名

首先我们在原有的StroyBoard的基础上,再新建两个StroyBoard文件,其中一个命名为first,一个命名为second。
这里写图片描述

再打开main.StroyBoard文件,拖入两个View,使用Segue进行连接,注意着两个Segue的命名,一定要和StroyBoard的名字一样(具体原因待会我会讲到),如下图所示。
这里写图片描述

重写UIStoryboardSegue类

新建Objective文件,继承UIStoryboardSegue

1
2
3
4
5
#import <UIKit/UIKit.h>
@interface JQStoryboardSegue : UIStoryboardSegue
@end

重写下面两个方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (instancetype)initWithIdentifier:(nullable NSString *)identifier source:(UIViewController *)source destination:(UIViewController *)destination {
UIStoryboard *externStroyBoard = [UIStoryboard storyboardWithName:identifier bundle:[NSBundle bundleForClass:self.class]];
id initViewController = [externStroyBoard instantiateInitialViewController];
return [super initWithIdentifier:identifier source:source destination:initViewController];
}
- (void)perform
{
[self.sourceViewController presentViewController:self.destinationViewController animated:YES completion:NULL];
}

从代码中我们可以来解释之前所说的为什么Segue的名字要和需要跳转的StoryBoard名字一样。[NSBundle bundleForClass:self.class] 这样就可以把这个新建的类抽象出来,适用于同样场景的跳转。perform方法指明了跳转的源控制器和目的控制器。自定义动画等也需要在这里实现,不过本文暂不介绍这方面的内容。

重写这个类之后,我们记得打开main.StoryBoard。把两个Segue的类修改为刚才新建的类。

这里写图片描述

Demo下载地址