How to inspect the responder chain?

前端 未结 6 1262
甜味超标
甜味超标 2020-12-08 10:51

I\'m doing some crazy multiple documents inside a single window stuff with the document-based architecture and I\'m 95% done.

I have this two-tier document architect

6条回答
  •  庸人自扰
    2020-12-08 11:18

    You can also add a category to class UIResponder with appropriate method that is possible to be used by any subclass of UIResponder.

    @interface UIResponder (Inspect)
    
    - (void)inspectResponderChain; // show responder chain including self
    
    @end
    
    @implementation UIResponder (Inspect)
    
    - (void)inspectResponderChain  
    {
        UIResponder *x = self;
        do {
            NSLog(@"%@", x);
        }while ((x = [x nextResponder]));
    }
    @end
    

    Than you can use this method somewhere in code as the example below:

    - (void)viewDidLoad {
        ...
        UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [self.view addSubview:myView];
        [myView inspectResponderChain]; // UIView is a subclass of UIResponder
        ...
    }
    

提交回复
热议问题