简体   繁体   English

iOS – 切换 UIWindow 时的解除分配问题

[英]iOS – Deallocation issue when switching UIWindow

I use to contextually switch key UIWindow s in my app to provide a bit cleaner flow – Welcome window => Main screen with items list <=> Item container with burger menu and stuff.UIWindow在我的应用程序中根据上下文切换关键UIWindow以提供更清晰的流程 - 欢迎窗口 => 带有项目列表的主屏幕 <=> 带有汉堡菜单和其他东西的项目容器。

Example function follows:示例函数如下:

- (void)updateKeyWindow:(UIWindow *)window withTransition:(WindowTransition)transition
{
    UIWindow *originalWindow = _keyWindow;
    _keyWindow = window;

    window.alpha = 0;
    [originalWindow resignKeyWindow];
    [originalWindow resignFirstResponder];
    originalWindow.userInteractionEnabled = NO;
    [window makeKeyAndVisible];
    window.transform = (transition == WindowTransitionFlyDown) ? CGAffineTransformMakeScale(1.02, 1.02) :
                       (transition == WindowTransitionFlyUp)   ? CGAffineTransformMakeScale(.96, .96) :
                                                                 CGAffineTransformIdentity;

//  [UIView animateWithDuration:.24 animations:^{

        window.alpha = 1;
        originalWindow.alpha = 0;
        window.transform = CGAffineTransformIdentity;
        originalWindow.transform = (transition == WindowTransitionFlyDown) ? CGAffineTransformMakeScale(.96, .96) :
                                   (transition == WindowTransitionFlyUp)   ? CGAffineTransformMakeScale(1.02, 1.02) :
                                                                             CGAffineTransformIdentity;

//  } completion:^(BOOL b){

        [originalWindow resignFirstResponder];
        [originalWindow removeFromSuperview];
        [originalWindow.rootViewController.view removeFromSuperview];
        originalWindow.rootViewController = nil;
        originalWindow = nil;

//  }];
}

I use animations to provide nice transitions, but I've commented it out to test if it's not the cause of the issue I have.我使用动画来提供漂亮的过渡,但我已将其注释掉以测试它是否不是我遇到的问题的原因。

The thing is, after dropping originalWindow from the hierarchy and quitting the block/function, the UIWindow is NOT being released and hangs somewhere in the space.问题是,从层次结构中删除originalWindow并退出块/函数后, UIWindow没有被释放并挂在空间的某个地方 I've tested this with child class by putting breakpoint inside overloaded -dealloc .我已经通过在重载的-dealloc放置断点来对子类进行测试。

I've checked both UIApplication 's -keyWindow and AppDelegate 's -window , both having new UIWindow object assigned.我检查了这两个UIApplication-keyWindowAppDelegate-window ,两者都具有新UIWindow分配的对象。

However after tapping anywhere on the screen, the -dealloc for the previous UIWindow is triggered with some -[UITouch dealloc] stuff in the call stack.然而,在点击屏幕上的任意位置后,前一个UIWindow-dealloc被调用堆栈中的一些-[UITouch dealloc]内容触发。

在新 UIWindow 内部敲击后调用堆栈触发旧 UIWindow 的释放

I find this behaviour completely weird, there must be something wrong within the UIKit , I'm not expecting there's anything wrong on my side with this approach.我发现这种行为非常奇怪, UIKit一定有问题,我不希望这种方法有任何问题。

Refer to the resignKeyWindow documentation :请参阅resignKeyWindow文档

Never call this method directly.切勿直接调用此方法。 The system calls this method and posts UIWindowDidResignKeyNotification to let the window know when it is no longer key...系统调用此方法并发布 UIWindowDidResignKeyNotification 以让窗口知道它何时不再是关键...

Try removing that and seeing if it fixes the crash.尝试删除它并查看它是否修复了崩溃。

The problem is UIApplication's keyWindow is weak by definition.问题是 UIApplication 的 keyWindow 根据定义很弱。 So you should have to connect your UIWindow's strongly on some object;所以你应该把你的 UIWindow 强烈地连接到某个对象上; like ApplicationDelegate or some singleton.像 ApplicationDelegate 或一些单例。

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

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