Kar*_*pez 6 memory-management objective-c viewcontroller ios
我真的很挣扎这个基本的iOS编程的东西,但我无法弄清楚发生了什么以及如何解决它.
我有我的主Login控制器,用于检测用户何时登录,如果auth成功,则显示下一个控制器:
@interface LoginViewController (){
//Main root instance
RootViewController *mainPlatformRootControler;
}
-(void)loggedInActionWithToken:(NSString *)token anonymous:(BOOL)isAnon{
NSLog(@"User loged in.");
mainPlatformRootControler = [self.storyboard instantiateViewControllerWithIdentifier:@"rootViewCOntrollerStoryIdentifier"];
[self presentViewController:mainPlatformRootControler animated:YES completion:^{
}];
}
Run Code Online (Sandbox Code Playgroud)
这很好,没问题.
我的麻烦是处理注销.如何完全删除RootViewController实例并显示一个新实例?
我可以看到RootViewController实例正在堆叠,因为我有多个观察者,并且在注销后然后登录它们被多次调用(多次我退出并重新进入).
我试过以下但没有成功:
首先检测RootViewController中的注销并解除:
[self dismissViewControllerAnimated:YES completion:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"shouldLogOut" object:nil];
}];
Run Code Online (Sandbox Code Playgroud)
然后在LoginViewController中:
-(void)shouldLogOut:(NSNotification *) not{
NSLog(@"No user signed in");
mainPlatformRootControler = NULL;
mainPlatformRootControler = nil;
}
Run Code Online (Sandbox Code Playgroud)
那我怎么办呢?我知道它是一个基本的内存处理器,但我只是不知道如何?
问题可能是您永远不会忽略RootViewController注销发生的时间。通过将该属性设置mainPlatformRootControler为nil,您只是从 的角度放弃了该对象的所有权LoginViewController。这并没有说明任何其他也拥有对后面对象的引用的东西mainPlatformRootControler。
要解决此问题,请在内部添加一个通知观察者RootViewController以获取注销通知,当收到通知时,通过以下方式自行关闭dismiss(animated:completion)
奖金mainPlatformRootControler如果你所做的只是保存它以将其清零,那么你也不需要该财产。通过正确地消除它(以我上面写的方式),它将自动清理,因此也不需要担心将nil其清除。(现在,如果您有其他原因保留mainPlatformRootControler,那么显然不要删除它)。
| 归档时间: |
|
| 查看次数: |
214 次 |
| 最近记录: |