How to Remove a Previous ViewController

前端 未结 5 1838
予麋鹿
予麋鹿 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:21

    For swift 3 you can replace

    self.present(ViewController(), animated: false, completion: nil);
    

    with

    if let window = UIApplication.shared.keyWindow{
                window.rootViewController = UINavigationController(rootViewController: ViewController());
            }
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-02-02 17:40

    Swift 3

    This will remove the current view controller from the memor

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        guard let viewControllers = navigationController?.viewControllers,
        let index = viewControllers.index(of: self) else { return }
        navigationController?.viewControllers.remove(at: index)
    }
    
    0 讨论(0)
  • 2021-02-02 17:40

    First of all, you are using the modal presentation pattern, rather than a navigation stack pattern (UINavigationController). You can't get rid of the presenting view controller, nor should you, as it would break navigation.

    The short answer here is that you don't need to worry about this. The amount of memory a view controller itself takes up is trivial and will be released automatically once there are no more references to it (one you dismiss it or pop it off a navigation stack).

    The longer answer would depend on your specific implementation. If you are somehow in a situation where you infinitely increasing the size of a stack, then it is a problem. An example would be if you have a navigation flow where you can freely move between two "screens" but internally each screen keeps presenting a new one rather than just switching to an existing one. If you are experiencing something like this, then you would be best suited to using a UITabBarController or something similar, where you are able to navigate laterally between view controllers without creating new ones.

    0 讨论(0)
  • 2021-02-02 17:41

    I guess best implementation is to place the following at the previous View Controller - Swift

        // Override to remove this view from the navigation stack
        override func viewDidDisappear(animated: Bool) {
            super.viewDidDisappear(animated)
            // Remove self from navigation hierarchy
            guard let viewControllers = navigationController?.viewControllers,
                let index = viewControllers.indexOf(self) else { return }
            navigationController?.viewControllers.removeAtIndex(index)
        }
    
    0 讨论(0)
提交回复
热议问题