正如其他人指出的那样,您不能使用segue推送到控制器的现有实例。执行segue的过程总是为您创建一个新的实例作为目标控制器。我个人认为,当我在视图控制器的现有实例之间跳转时,我认为“容器视图控制器”(例如UIPageViewController
)可以很容易地在两个或多个控制器之间转换,而不必每次都重新实例化它们。
如果您不喜欢页面视图控制器强加的约束(例如,您可能不喜欢iOS 5版本仅支持页面卷曲转换,或者iOS 6只添加滚动转换,并且您想要别的东西),那么你会做一个自定义的容器视图控制器。例如,如果我想在两个视图控制器之间跳转,并且不会每次重新实例化它们,我首先会创建一个自定义容器视图控制器,即“父级”,并确保我有一个属性以跟踪哪个孩子我目前在:
@property (nonatomic) NSInteger childViewIndex;
如果支持iOS 6。只有0和以上,我会然后添加一个“容器视图”到我的父视图控制器的场景。如果以6.0之前支持IOS版本,我会一个标准UIView
添加到场景中,然后手动实例化的第一个孩子控制器:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIViewController *controller;
// add the first child
UIViewController *controller = [self addChildWithIdentifier:@"One"];
[self.containerView addSubview:controller.view];
[controller didMoveToParentViewController:self];
self.childViewIndex = 0;
}
- (UIViewController *)addChildWithIdentifier:(NSString *)storyboardIdentifier
{
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:storyboardIdentifier];
[self addChildViewController:controller];
controller.view.frame = self.containerView.bounds;
controller.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
return controller;
}
然后,当我想过渡到第二个孩子(或过渡回第一个孩子),我会打电话给父视图控制器中执行以下程序:
- (void)transitionToViewControllerIndex:(NSInteger)index
{
// don't do anything if we're trying to transition to ourselves!
if (index == self.childViewIndex)
return;
// identify the two controllers in question
UIViewController *sourceController = self.childViewControllers[self.childViewIndex];
UIViewController *destinationController;
// if we're asking for page 2, but we only have one defined, then we'll have to instantiate it
BOOL instantiateDestination = (index == 1 && [self.childViewControllers count] < 2);
if (instantiateDestination)
destinationController = [self addChildWithIdentifier:@"Two"];
else
destinationController = self.childViewControllers[index];
// configure the destination controller's frame
destinationController.view.frame = sourceController.view.frame;
// if you're jumping back and forth, set the animation appropriate for the
// direction we're going
UIViewAnimationOptions options;
if (index > self.childViewIndex)
{
options = UIViewAnimationOptionTransitionFlipFromRight;
}
else
{
options = UIViewAnimationOptionTransitionFlipFromLeft;
}
// now transition to that destination controller
[self transitionFromViewController:sourceController
toViewController:destinationController
duration:0.5
options:options
animations:^{
// for simple flip, you don't need anything here,
// but docs say this can't be NULL; if you wanted
// to do some other, custom annotation, you'd do it here
}
completion:^(BOOL finished) {
if (instantiateDestination)
[destinationController didMoveToParentViewController:self];
}];
self.childViewIndex = index;
}
因此,过渡到第二子视图控制器,你可以直接电话咨询:
[self transitionToViewControllerIndex:1];
如果你想转换回,你可以拨打:
[self transitionToViewControllerIndex:0];
我只在这里划伤表面,但容器视图控制器(或者如果没有通用的标准为你做的工作,一个自定义的容器视图控制器)正是你需要的。
欲了解更多信息,请参见:
来源
2013-04-27 18:36:01
Rob
下面是如何在代码完全操作导航界面我的书的讨论:http://www.apeth.com/iOSBook/ch19.html#_configuring_a_navigation_controller – matt 2013-04-27 16:18:33
谢谢!成功管理您的工作和@ rdelmar的答案。我已经将“Next”按钮操作更改为标准IBAction,以便推送所需的视图控制器而不是使用segues - 这样,我可以将实例化视图控制器存储在堆栈中的前一个视图控制器中,并在实例化视图控制器时将其推送存在。我同意故事板掩盖了很多正在发生的事情,这导致了对低层的理解不足。我会标记你的答案,当它让我:) – 2013-04-27 16:47:54
你可能想要尝试'setViewControllers:animated:'。它可以让你重新安排现有的视图控制器*和*可能添加更多的视图控制器,删除现有的视图控制器等,*和*它执行自动动画(除非你不告诉它)。因此,您可以将两个备用视图控制器保留在堆栈中,并重新排列它们。 – matt 2013-04-27 16:56:44