且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除以前的ViewController

更新时间:2023-12-03 18:42:16

我可以假设,查看屏幕上显示的控制器自动从主故事板或通过设置应用程序的 window.rootViewController 属性进行实例化。

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.

在任何一种情况下,您都可以再次设置 rootViewController 作为 vc 。要更改应用程序的rootViewController,您需要替换以下代码行:

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.

Objective-C

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

Swift

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



使用过渡动画导航:



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
    })

备注:
一旦rootViewController值被更改,原始视图控制器引用计数应该变为0,因此它将从内存中删除!

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