正如其他人指出的那樣,您不能使用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