有没有办法检测当前可见的 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屋!