简体   繁体   English

Objective-C:模态搜索和发布视图控制器

[英]Objective-C : Modal Segues and Releasing View Controller

The initial view controller on my app is a UITabBarController that displays for logged in users. 我的应用程序上的初始视图控制器是UITabBarController,它为登录的用户显示。

For new users, however, my app delegate will point them to a login/registration view controller first: 但是,对于新用户,我的应用程序委托将首先将他们指向登录/注册视图控制器:

// New user, show login
self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];

When the user has completed the login/registration, I then send the user to the tabbar as such: 用户完成登录/注册后,我将用户发送到标签栏,如下所示:

// Login done, go to main view
[self performSegueWithIdentifier:@"userLoginToMainSeg" sender:self];

However, doing it this way, the LoginViewController is not released (dealloc is not called). 但是,通过这种方式,不会释放LoginViewController(不会调用dealloc)。

Can someone explain the error in my logic here? 有人可以在这里解释我的错误吗?

Your modal segue is basically doing: 您的情态搜索基本上是在做:

[loginViewController presentViewController:mainViewController animated:YES completion: ...];

What this means is that mainViewController becomes loginViewController s presentedViewController : 这意味着mainViewController成为loginViewControllerpresentedViewController

loginViewController.presentedViewController == mainViewController
mainViewController.presentingViewController == loginViewController

When you're presenting a view controller , the presenting view controller remains in the view controller hierarchy, so that you can later navigate back by calling: 当您呈现视图控制器时 ,呈现的视图控制器将保留在视图控制器层次结构中,以便您以后可以通过调用以下内容进行导航:

[loginViewController dismissViewControllerAnimated: ...];

So it's perfectly normal that loginViewController is not released, since it's still the window's rootViewController . 因此,不释放loginViewController完全正常,因为它仍然是窗口的rootViewController It's only that loginViewController is obstructed by the presented mainViewController . 出现的loginViewController只是阻塞了mainViewController

If you want to eradicate loginViewController you can set window.rootViewController directly, but that wouldn't animate the transition. 如果您想消除loginViewController ,可以直接设置window.rootViewController ,但这不会为过渡设置动画。 You can achieve animation by messing around the view controllers' views , but it's kind of outside the officially sanctioned territory... 您可以通过弄乱视图控制器的视图来获得动画,但这有点超出了官方批准的范围...

IMO the cleanest solution would be to implement a basic container view controller that would be your window's rootViewController , and that could orchestrate the transition between loginViewController and mainViewController by animating their views, and then throwing away loginViewController . IMO的最干净的解决方案是实现一个基本的容器视图控制器 ,该控制器将成为您窗口的rootViewController ,并且可以通过动画化视图的动画,然后扔掉loginViewController ,来协调loginViewControllermainViewController之间的过渡。 It would be kind of a primitive navigation controller without a navigation bar and a navigation stack – just swapping the current view controller with the new one, and throwing away the former. 这将是一种没有导航栏和导航堆栈的原始导航控制器,只需将当前的视图控制器替换为新的视图控制器,然后丢弃前者即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM