有没有办法检测什么UIView当前可见? [英] Is there a way to detect what UIView is currently visible?
问题描述
我有一个类与Web服务进行通信,并在整个应用程序中使用。我正在寻找一种方法来显示一个错误消息在UIActionSheet在任何视图的用户之上。有一个简单的方法这样做吗?
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 what ever view the user is in. Is there an easy way to do this? I would like to avoid call back methods in every view if at all possible.
推荐答案
如果您在其他答案中使用代码,当您的应用程序提交到应用商店(使用非公共API)时,将被拒绝。我发现了硬的方式。一个更好的解决方案是创建一个类别。下面是我用来替换原始解决方案中的代码:
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
和
@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
这篇关于有没有办法检测什么UIView当前可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!