简体   繁体   English

在没有层次结构的两个视图控制器之间切换

[英]Switching between two view controllers without hierarchy

I have a few ViewControllers that all have buttons which should segue to some others. 我有几个ViewControllers都有应该与其他按钮连接的按钮。 There will never be a back button, but instead everything is connected through a bunch of loops so that there is never a dead end. 永远不会有后退按钮,而是所有东西都通过一堆循环连接在一起,因此永远不会死胡同。 So I'd like to fully transition from one View Controller to another, and have the old View Controller be completely deleted. 因此,我想从一个View Controller完全过渡到另一个View Controller,并完全删除旧的View Controller。 There is no hierarchy and no parent/child relationship between the View Controllers. 视图控制器之间没有层次结构,也没有父/子关系。 How should I handle this situation? 我应该如何处理这种情况?

Instantiate the view controller you want to go to, then set it as the window's root view controller. 实例化要转到的视图控制器,然后将其设置为窗口的根视图控制器。

NextViewController *next = [self.storyboard instantiateViewControllerWithIdentifier:@"Next"]; // or other instantiation method depending on how you create your controller
self.view.window.rootViewController = next;

You could do this with custom segues if you want to show the flow from controller to controller in your storyboard (you wouldn't need any code at all then). 如果要在情节提要中显示控制器之间的流,则可以使用自定义序列来执行此操作(那时根本不需要任何代码)。 The custom segue's perform method would look like this, 自定义segue的perform方法看起来像这样,

@implementation RootVCReplaceSegue

-(void)perform {
    UIViewController *source = (UIViewController *)self.sourceViewController;
    source.view.window.rootViewController = self.destinationViewController;
}

If you want a fade animation, you can add a snapshot of the source view controller as a subview of the destination view controller's view, then fade it out, 如果您想要淡入淡出的动画,则可以将源视图控制器的快照添加为目标视图控制器的视图的子视图,然后使其淡出,

-(void)perform {
    UIViewController *source = (UIViewController *)self.sourceViewController;
    UIView *sourceView = [source.view snapshotViewAfterScreenUpdates:YES];
    [[self.destinationViewController view] addSubview:sourceView];
    source.view.window.rootViewController = self.destinationViewController;
    [UIView animateWithDuration:.5 animations:^{
        sourceView.alpha = 0;
    } completion:^(BOOL finished) {
        [sourceView removeFromSuperview];
    }];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM