willMoveToWindow 被调用两次 [英] willMoveToWindow is called twice

查看:58
本文介绍了willMoveToWindow 被调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 willMoveToWindow: 并且我遇到了一个问题,它在视图上被调用了两次.

I'm swizzling willMoveToWindow: and I came across an issue where it was being called twice on views.

当一个新的视图控制器被推送到一个 UINavigationController 上时,

When a new view controller is pushed onto a UINavigationController,

willMoveToWindow: 使用 nil 值在现有视图上调用(有意义,因为视图移出屏幕)

willMoveToWindow: is called on the existing view with nil value (Makes sense since the view is moving offscreen)

在那之后,方法 willMoveToWindow: 再次被调用,但现在使用原始窗口.

After that time, the method willMoveToWindow: is called again , but now with with the original window.

我最初的想法是在原始方法开始之前调配并调用 window 属性.

My initial thought is that swizzling and calling the window property before the original method has kicked in.

为了安全起见,我创建了一个小型示例项目并确认了相同的行为.

To be on the safe side I've created a small sample project and confirmed the the same behavior.

基本上我需要一种方法来确定视图不在 window 上(因为当视图移动到实际上不应该运行的窗口时,我正在触发逻辑(在至少不是两次))

Basically I need a way to know for sure that the view is not on the window (Because I am triggering logic when a view is moving to window which in reality shouldn't be run (at least not twice))

作为参考,可以使用以下代码重现该问题:

For reference the problem can be reproduced with the following code:

  @implementation RandomView

    -(void)willMoveToWindow:(UIWindow *)newWindow {
    // when the new view controller is pushed - 
    //the method is called twice on the existing view (on the screen view)- 
    //first time will be called with nil - 
    //second time with the original window
        NSLog(@"********%s <RandomView %p> <Window %p>",__PRETTY_FUNCTION__,self,newWindow);
    }

    -(void)didMoveToWindow {
        NSLog(@"********%s <RandomView %p> <Window %p>",__PRETTY_FUNCTION__,self,self.window);
    }
    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        RandomView *k = [[RandomView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:k];
    }


    -(void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                ViewController *vc = [[ViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            });
        });

        //[self becomeFirstResponder];
    }
    @end

编辑控制台

    [RandomView willMoveToWindow:] <RandomView 0x7f8b21e16630> <Window 0x7f8b21d220d0>
    [RandomView didMoveToWindow] <RandomView 0x7f8b21e16630> <Window 0x7f8b21d220d0>
  //THIS IS THE ISSUE
    [RandomView willMoveToWindow:] <RandomView 0x7f8b21e16630> <Window 0x0>
    [RandomView didMoveToWindow] <RandomView 0x7f8b21e16630> <Window 0x0>
    [RandomView willMoveToWindow:] <RandomView 0x7f8b21e16630> <Window 0x7f8b21d220d0>
    [RandomView didMoveToWindow] <RandomView 0x7f8b21e16630> <Window 0x7f8b21d220d0>

    [RandomView willMoveToWindow:] <RandomView 0x7f8b21e16630> <Window 0x0>
    [RandomView didMoveToWindow] <RandomView 0x7f8b21e16630> <Window 0x0>

推荐答案

我也面临同样的问题

但是在通过响应者链查找视图层次结构后,可以检查一个小的不同.我不确定这是否安全.

but after lookup the view hierarchy through responder chain, there is a small different can check. I am not sure this is safe or not.

但我认为 Apple 的动画代码顺序错误,如果他们先将动画视图添加到窗口,willMoveToWindow: 将不会调用两次.

but I think Apple did the animation code in wrong order, if they add the animation view to window first, the willMoveToWindow: will not call twice.

2017-03-08 22:49:35.167 view[36189:410065] show
0x7fa9c36059f0,MyView
0x7fa9c3407cb0,UIView
0x7fa9c340b9d0,ViewController
0x7fa9c3403c50,UIViewControllerWrapperView
0x7fa9c340ee90,UINavigationTransitionView
0x7fa9c5802d10,UILayoutContainerView
0x7fa9c381ee00,UINavigationController
0x7fa9c3609c40,UIWindow
0x7fa9c3400020,UIApplication
0x608000038900,AppDelegate
2017-03-08 22:49:54.501 view[36189:410065] hide
0x7fa9c36059f0,MyView
0x7fa9c3407cb0,UIView
0x7fa9c340b9d0,ViewController
0x7fa9c3500bd0,UIView  <----- not real hide
2017-03-08 22:49:54.501 view[36189:410065] show
0x7fa9c36059f0,MyView
0x7fa9c3407cb0,UIView
0x7fa9c340b9d0,ViewController
0x7fa9c3500bd0,UIView
0x7fa9c3403c50,UIViewControllerWrapperView
0x7fa9c340ee90,UINavigationTransitionView
0x7fa9c5802d10,UILayoutContainerView
0x7fa9c381ee00,UINavigationController
0x7fa9c3609c40,UIWindow
0x7fa9c3400020,UIApplication
0x608000038900,AppDelegate
2017-03-08 22:49:54.501 view[36189:410065] show
0x7fa9c35062f0,MyView
0x7fa9c3505ae0,UIView
0x7fa9c58030c0,ViewController
0x7fa9c3506c10,_UIParallaxDimmingView
0x7fa9c35022c0,UIView
0x7fa9c3403c50,UIViewControllerWrapperView
0x7fa9c340ee90,UINavigationTransitionView
0x7fa9c5802d10,UILayoutContainerView
0x7fa9c381ee00,UINavigationController
0x7fa9c3609c40,UIWindow
0x7fa9c3400020,UIApplication
0x608000038900,AppDelegate
2017-03-08 22:49:55.037 view[36189:410065] hide
0x7fa9c36059f0,MyView
0x7fa9c3407cb0,UIView
0x7fa9c340b9d0,ViewController  <----- real hide

这篇关于willMoveToWindow 被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆