How to Remove a Previous ViewController

前端 未结 5 1824
予麋鹿
予麋鹿 2021-02-02 16:48

I am a student and pretty new to programming. I am trying to learn Objective-C/Swift in my spare time. I made a game using spriteKit with swift that has multiple menus/scenes. <

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-02 17:32

    As I can assume, view controller being presented on the screen was instantiated either automatically from main storyboard or by setting app's window.rootViewController property.

    In either case you can set rootViewController again to be your vc. To change rootViewController of your app you need to replace this line of code:

    self.presentViewController(vc, animated: true, completion: nil)
    

    ... with one of the options bellow.

    "Navigate" without transition animation:

    Objective-C

    UIWindow *window = (UIWindow *)[[UIApplication sharedApplication].windows firstObject];
    window.rootViewController = vc;
    

    Swift

    let window = UIApplication.sharedApplication().windows[0] as UIWindow;
    window.rootViewController = vc;
    

    "Navigate" with transition animation:

    Objective-C

    UIWindow *window = (UIWindow *)[[UIApplication sharedApplication].windows firstObject];
    [UIView transitionFromView:window.rootViewController.view
                        toView:vc.view
                      duration:0.65f
                       options:UIViewAnimationOptionTransitionCrossDissolve // transition animation
                    completion:^(BOOL finished){
                        window.rootViewController = vc;
                    }];
    

    Swift

    let window = UIApplication.sharedApplication().windows[0] as UIWindow;
    UIView.transitionFromView(
        window.rootViewController.view,
        toView: vc.view,
        duration: 0.65,
        options: .TransitionCrossDissolve,
        completion: {
            finished in window.rootViewController = vc
        })
    

    Remarks: Once rootViewController value gets changed your original view controller reference count should became 0 hence it will be removed from the memory!

提交回复
热议问题