在 Storyboard 里我们可以创建一个个 Scene,其中,每个 Sence 代表了一个 View Controller 。而在 View Controller 之间跳转则可以使用 Segue。
创建 Scene
创建这种工作在 Storyboard 里是非常简单的——只要拖拖拖就可以了。
所以,创建一个 Scene,只需要将 View Controller 从 Object library 拖到中间的空白区域就可以了。
自定义 View Controller
当然,选中不同的控件都可以在右边修改其属性。
获取 Storyboard 中的 ViewController
我们可以使用 UIStoryboard 中的:
// Identifier 为 Storyboard 中设置的值
- (__kindof UIViewController *)instantiateViewControllerWithIdentifier:(NSString *)identifier;例如:
//获取当前 ViewController 所在的 Storyboard 中的 ViewController
UIViewController* viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"View Controller Identifier"];
//或其他 Storyboard 中的 ViewController
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:bundle];
UIViewController* viewController = [storyboard instantiateViewControllerWithIdentifier:@"View Controller Identifier"];所以我们也可以直接使用代码来获取 View Controller 并完成跳转:
[self showViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View Controller Identifier"] sender:sender];初始场景
每一个 Storyboard 文件都应有一个初始场景,我们可以在 Attributes Inspector 里勾选 Is Initial View Controller 或拖动 Scene 左边的渐进短箭头至另一个 Scene 来使一个 View Controller 成为初始场景。
使用 Segue 实现场景跳转
场景间的跳转也很简单,只要按住 control 键或使用辅助按键从一个 UIButton 或 View Controller 拖线至另一个 ViewController 中,然后选择呈现方式即可。
我们同样可以选中 Segue 后在 Attributes Inspector 中更改其属性,如 Identifier 、Kind 等。
如果是从 UIButton 中拖过去的,那么点击该 UIButton 便可以以选择的呈现方式跳转至链接场景。如果是从一个 View Controller 拖至另一个 View Controller ,点击 View Controller 是不会自动跳转到,这时候我们需要手动触发 Segue :
[self performSegueWithIdentifier:@"Segue Identifier" sender:sender];返回场景
跳转场景后返回到原来的场景时自然不能再新创建一个 Segue,我们可以使用以下方法完成:
在需要返回到的 ViewController 中实现如下方法:
- (IBAction)unwindForSegue:(UIStoryboardSegue *)unwindSegue towardsViewController:(UIViewController *)subsequentVC;方法名可以任意修改,但参数及返回值不可以修改。
例如:我们需要返回至 SecondViewController ,那么我们可以在 SecondViewController 中实现如下方法:- (IBAction)unwindToSecondViewController:(UIStoryboardSegue *)unwindSegue towardsViewController:(UIViewController *)subsequentVC { //code }- 在 StoryBoard 中从一个 UIButton 处拖线至 UIButton 所在 Scene 的顶部的 Exit 按钮上,然后选择上面的方法即可。
![]()
使用 Segue 传递数据
除了 delegate 模式,我们还可以使用 Segue 来传递数据。
例如:我们现在需要在需要从 SecondViewController 中跳转到 FinalViewController,并且在跳转前传递数据至 FinalViewController,那么我们可以这么做:
- 在 Storyboard 中创建从 SecondViewController 中跳转到 FinalViewController 的 Segue ,并设置其 Identifier。
在 SecondViewController 中重写如下方法:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"Segue Identifier"]) { //set data segue.destinationViewController.someData = data; [segue.destinationViewController someMethod:param]; } }控制 Segue 是否应该被触发
可以重写如下方法,不重写则默认返回为
YES.(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
return YES;
}## Storyboard Reference 当项目变得越来越庞大时,将所有 Scene 放在一个 Storyboard 文件里将会非常麻烦,且多人协作时也会有诸多不便。这时,我们就可以创建多个 Storyboard 文件,并在需要时创建其引用(Storyboard Reference)同一个 Storyboard 文件可以被引用多次。比如将某模块中的所有 Scene 放在 Module1.storyboard 文件里,并在主 Main Storyboard 里创建其引用。 创建 Storyboard Reference 的方法有两个。- 选中需要移动的 Scene(可以多选)
- 在菜单栏依次选中
Editor -> Refactor to Storyboard... - 在弹出的对话框中输入新的 Storyboard 文件名
- 保存
第二种:新建 Storyboard 文件并在需要的地方创建其引用
- 新建一个 Storyboard 文件。
- 在需要创建引用的地方从 Object library 将 Storyboard Reference 拖到适当的地方。
- 选中刚刚创建的 Storyboard Reference ,在 Attributes Inspector 里设置引用的 Storyboard 为之前新建的 Storyboard 文件。
- 如果需要,创建一个 Segue 指向刚刚创建的 Storyboard Reference。
默认跳转至新建的 Storyboard 的初始场景,当然,我们也可以在 Attributes Inspector 里进行修改。
