希望在iPhone故事板应用程序中最大化重用

Alp*_*per 2 iphone xcode storyboard ios

我正在编写一个复杂的故事板应用程序,当遇到跨视图控制器和不同应用程序路径的代码重用时,我遇到了一些问题.

我可以转到没有直接连接到当前的ViewController吗?

如何有条件地从按钮切换到多个ViewControllers?连接两者都不起作用.

我可以从应用程序中的任意位置输入ViewController序列吗?

只有几个问题出现了.有人有任何想法或好例子吗?

Mat*_*uch 6

我可以转到没有直接连接到当前的ViewController吗?

没有segue.但是,您可以模拟地从故事板中推送或呈现viewController,就像使用.xib文件一样.

// if self was created from code or from a .xib:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil];
// if self was instantiated from within a storyboard:
UIStoryboard *storyBoard = self.storyboard;  

MyFancyViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"MyFancyViewControllerIdentifier"];
[self presentModalViewController:viewController animated:YES];
Run Code Online (Sandbox Code Playgroud)

但是你必须先设置标识符.在故事板中选择viewController,然后在属性检查器中执行此操作.

一旦MyFancyViewController可见,您就可以使用它的所有segue.在代码和故事板段之间切换可能会感到奇怪,但这并没有错.它使整个故事板的东西真的可用.


如何有条件地从按钮切换到多个ViewControllers?连接两者都不起作用.

添加两个或多个从viewController开始的segue(不是从按钮或任何其他视图开始.例如,在状态栏上开始控制 - 拖动)到目标viewControllers.为它们设置标识符并使​​用如下代码:

if (someState) {
    [self performSegueWithIdentifier:@"MyFirstSegueIdentifier" sender:somethingOrNil];
}
else {
    [self performSegueWithIdentifier:@"MySecondSegueIdentifier" sender:somethingOrNil];
}
Run Code Online (Sandbox Code Playgroud)

我可以从应用程序中的任意位置输入ViewController序列吗?

是的,如果你使用"老派"视图控制器管理.与第一部分类似.实例化您的viewController并使用代码呈现它.