正如我可以假设的那样,屏幕上呈现的视图控制器是从主故事板自动实例化的,或者是通过设置应用程序的window.rootViewController
财产。
无论哪种情况,您都可以设置rootViewController
再次成为你的vc
。要更改应用程序的 rootViewController,您需要替换以下代码行:
self.presentViewController(vc, animated: true, completion: nil)
...使用以下选项之一。
没有过渡动画的“导航”:
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
})
Remarks:一旦 rootViewController 值发生更改,您的原始视图控制器引用计数应该变为 0,因此它将从内存中删除!