如何删除以前的ViewController [英] How to Remove a Previous ViewController
问题描述
我是一名学生,对编程很陌生。我想在业余时间学习Objective-C / Swift。我使用带有多个菜单/场景的swift的spriteKit制作了一个游戏。
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.
我正在尝试从一个视图控制器转换到另一个视图控制器。为此,我使用了以下代码:
I am trying to transition from one view controller to another. To do this I used this code:
@IBAction func PlayButtonPressed(sender: AnyObject) {
let playStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc : UIViewController = playStoryboard.instantiateViewControllerWithIdentifier("playGame") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
这适用于过渡到新的VC场景,不过,我相信以前的VC仍然在堆栈中并占用内存,从而减慢了我的程序速度。
This works for transitioning to the new VC scene, however, I believe the previous VC is still in the stack and takes up memory, slowing down my program.
我读过其他一些帖子,你可以使用导航控制器删除VC。但是,我没有导航控制器;只查看控制器。我见过一些关于 removeFromParentViewController()
和 view.removeFromSuperview()
的东西,但我真的不知道如何实现它。除此之外我找不到我想要的答案。
I've read on some other posts that you can use the navigation controller to remove VC. However, I do not have a navigation controller; only view controllers. I've seen some stuff about removeFromParentViewController()
and view.removeFromSuperview()
, but I don't really know how to implement it. Other than that I did not find an answer that I was looking for.
所以我问的问题是如何从堆栈中删除以前的VC?任何帮助将是一个赞赏! (希望帮助很快,但Objective-C也有帮助)提前感谢你!
So the question I am asking is how do I remove the previous VC from the stack? Any help would be a appreciated! (would prefer the help to be in swift, but Objective-C would help also) Thank you in advance!
注意参考:我相信Objective-C我的代码看起来像这样:
note for reference: I believe in Objective-C my code would look something like this:
-(IBAction) PlayButtonPressed: (id) sender {
UIStoryboard *playStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [playStoryboard instantiateViewControllerWithIdentifier:@"playGame"];
[self presentViewController:vc animated:YES completion:nil];
}
推荐答案
我可以假设,查看屏幕上显示的控制器自动从主故事板或通过设置应用程序的 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!
这篇关于如何删除以前的ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!