Apple会拒绝手动调用viewDidUnload的应用程序

时间:2012-10-10 21:19:58

标签: ios memory-management ios6 appstore-approval viewdidunload

在iOS6中不再调用viewDidUnload,因此作为在viewDidUnload中执行某些必要操作的应用程序的变通方法,我已完成此操作:

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];


    // only want to do this on iOS 6
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0) {
        //  Don't want to rehydrate the view if it's already unloaded
        BOOL isLoaded = [self isViewLoaded];

        //  We check the window property to make sure that the view is not visible
        if (isLoaded && self.view.window == nil) {

            //  Give a chance to implementors to get model data from their views
            [self performSelectorOnMainThread:@selector(viewWillUnload)
                                   withObject:nil
                                waitUntilDone:YES];

            //  Detach it from its parent (in cases of view controller containment)
            [self.view removeFromSuperview];
            self.view = nil;    //  Clear out the view.  Goodbye!

            //  The view is now unloaded...now call viewDidUnload
            [self performSelectorOnMainThread:@selector(viewDidUnload)
                                   withObject:nil
                                waitUntilDone:YES];
        }
    }
}

Apple拒绝这样的事先有先例吗?由于时间限制,我不能冒险拒绝任何事情。

2 个答案:

答案 0 :(得分:1)

Apple没有理由拒绝它,但是他们之所以将其删除是有原因的。

手动调用只是问问题。我强烈建议让应用程序逻辑正确,并以“正确”的方式进行。

编辑:再次查看该代码后,我对您的实施存在严重怀疑。带有调用选择器的整个构造(我们应该已经在主线程上了!)并从su​​perview中删除(只是从它的所有者那里窃取视图而不告诉它)只是不正确。这是您真正希望从代码库中消除的代码类型。

答案 1 :(得分:0)

他们没有理由拒绝这一点。通过弃用该方法,-[UIView viewDidUnload]就像任何其他方法一样。您可以将其视为首先在UIView上从未存在过,而您恰好创建了一个名为-viewDidUnload的方法。

如果-viewDidUnload仍然存在于内部(它可能)并且您试图调用Apple(现在是私有的)实现而不是您自己的实现,会出现问题,但我非常怀疑Apple会不会做这个。请记住在你的-viewDidUnload中记住要求超级类在尝试在super上调用它之前是否实现该方法,如果这是你当前正在做的事情,请使用:

if ([[self superclass] instancesRespondToSelector:@selector(viewDidUnload)]) {
    [super viewDidUnload];
}

如果您真的想要安全,可以随时将代码移动到其他方法。在viewDidUnload内部,只需为iOS 5设备调用新方法,如果您使用的是iOS 6,则在-didReceiveMemoryWarning调用新方法。

我不打算详细评论你的didReceiveMemoryWarning中的逻辑,因为那不是问题,但我会说你应该非常小心你把控制器放在哪个状态(确保那些if语句涵盖了你的所有基础!)当然,当你调用viewDidUnload时,你不能指望你的视图控制器及其视图处于相同的状态,就像在iOS 5中由UIKit调用它一样。