正如其他人指出的那样,您不能使用 segue 推送到控制器的现有实例。执行 segue 的过程总是会为您创建一个目标控制器的新实例。
就个人而言,当我在视图控制器的现有实例之间跳转时,我认为“容器视图控制器”,例如 a 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];
我在这里只是触及表面,但容器视图控制器(或者如果没有标准的控制器为您完成这项工作,自定义容器视图控制器)正是您所需要的。
有关更多信息,请参阅: