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.
+6
A:
What you want to do is find the first responder of the key window I would think. You can do that like this:
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
That should give you the view to use in your call to the UIActionSheet.
littleknown
2009-06-04 12:06:36
That did it. Thanks.
Kevin
2009-06-09 11:29:30
Just thought you would like to know that if you submit your app with the code above, it will get rejected for use a non-public api. It happened to me.
Peter Parker
2009-12-01 00:13:41
+3
A:
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:
@implementation UIView (FindFirstResponder)
- (UIView *)findFirstResonder
{
if (self.isFirstResponder) {
return self;
}
for (UIView *subView in self.subviews) {
UIView *firstResponder = [subView findFirstResonder];
if (firstResponder != nil) {
return firstResponder;
}
}
return nil;
}
@end
Peter Parker
2009-12-01 02:14:00
A:
Hi Anthony D,
How do I use your code above?
Do I put your code in a new .m file?
how do I call it to check for subviews?
Thanks
Sorry, just saw you question. Do some research on Categories. After that, you should understand how to use the code.
Peter Parker
2010-01-29 16:45:26