Is there a way to detect what UIView is currently visible?

后端 未结 2 1674
悲&欢浪女
悲&欢浪女 2020-12-15 01:58

I have a class that communicates with a web service and is used throughout the app. What I am looking for is a way to display an Error message in a UIActionSheet on top of w

2条回答
  •  时光说笑
    2020-12-15 02:42

    If you use the code in the other answer, your app will get rejected when submitted to the app store (for using a non-public api). I found that out the hard way. A better solution is to create a category. Here is what I used to replace the code in the original solution:

    @interface UIView (FindFirstResponder)
    - (UIView *)findFirstResponder;
    @end
    

    And

    @implementation UIView (FindFirstResponder)
    - (UIView *)findFirstResponder
    {
        if (self.isFirstResponder) {        
            return self;     
        }
    
        for (UIView *subView in self.subviews) {
            UIView *firstResponder = [subView findFirstResponder];
    
            if (firstResponder != nil) {
                return firstResponder;
            }
        }
    
        return nil;
    }
    @end
    

提交回复
热议问题