UIViewController 没有自行解除分配

UIViewController did not deallocate itself

我正在开发一个应用程序,它根据它的状态改变它的 rootViewController。要进行切换,我使用此代码:

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self createManagedDocumentAndContext];

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *storyboardId = [userDefaults boolForKey:@"Profile Created"] ? @"User Stats" : @"Profile";

    self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:storyboardId];

    return YES;
}

要切换回来,我从提供的配置文件中调用此方法VC:

- (void)returnOldRootViewController
{
    UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    OLDUserStatsVC *userStatsVC = [storyboard instantiateViewControllerWithIdentifier:@"User Stats"];
    userStatsVC.userProfile = self.userProfile;

    [currentWindow setRootViewController:userStatsVC];

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:YES forKey:@"Profile Created"];

}

问题: rootViewController 已更改,但前一个未解除分配。它保留在应用程序的 "background" 上 - 当 VC 更改为另一个应用程序时我可以看到它。

问题是如何正确释放? 非常感谢!

这里真正的问题是您正在更改 window 的根视图控制器。不要那样做。您应该 设置一次 并且永远不会再设置。在应用程序的生命周期中应该只有 一个 根视图控制器。

寻找另一种架构来显示正确的视图控制器并根据需要在它们之间切换。例如,它们可能都是根视图控制器的子级,或者一个可能是另一个前面的呈现视图控制器。