views:

752

answers:

4

Does anyone know in whose subview an active UIAlertView is located or how to find the thread in which it is running?

+3  A: 

The active UIAlertView lives in a separate window (_UIAlertOverlayWindow). Use .windows property to find it.

The whole UI runs in the main thread.

KennyTM
+4  A: 

If you dump the contents of the windows property and all subviews of all views you can see that the UIAlertView is in a separate window that overlays the main window. Here I have a navbar with a viewcontroller and a tableview (I removed its subviews since they're not relevent).

<UIWindow: 0x411fd50; frame = (0 0; 320 480); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x4120af0>>
: <UILayoutContainerView: 0x4123310; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x411f800>>
: | <UINavigationTransitionView: 0x4123500; frame = (0 0; 320 480); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x41232e0>>
: | : <UIViewControllerWrapperView: 0x4519d30; frame = (0 64; 320 416); autoresize = W+H; layer = <CALayer: 0x4519a40>>
: | : | <UITableView: 0x7808000; frame = (0 0; 320 416); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x45182a0>>
: | <UINavigationBar: 0x45018b0; frame = (0 20; 320 44); clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x4500fe0>>
: | : <UINavigationItemView: 0x4522a20; frame = (100 8; 160 27); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4526310>>
: | : <UINavigationItemButtonView: 0x45230a0; frame = (5 7; 87 30); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4520260>>
<_UIAlertOverlayWindow: 0x4179b70; frame = (0 0; 320 480); opaque = NO; layer = <CALayer: 0x4188dc0>>
: <UIAlertView: 0x4194bc0; frame = (3.8 161.95; 312.4 177.1); transform = [1.1, 0, 0, 1.1, 0, 0]; opaque = NO; animations = { transform=<CABasicAnimation: 0x4191160>; opacity=<CABasicAnimation: 0x41226f0>; }; layer = <CALayer: 0x4144c30>>
: | <UILabel: 0x4177e70; frame = (12 15; 260 23); text = 'Name of Date'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4179370>>
: | <UILabel: 0x418b100; frame = (12 45; 260 41); text = 'Name of the date that you...'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4128450>>
: | <UIThreePartButton: 0x41942a0; frame = (11 102; 262 43); opaque = NO; tag = 1; layer = <CALayer: 0x4191f30>>

Here is the code that produces the dump. I found it useful on occasion to see what is going on when something isn't doing what I expect:

void dumpView(UIView* aView, NSString* indent) {
    if (aView) {
        NSLog(@"%@%@", indent, aView);      // dump this view

        if (aView.subviews.count > 0) {
            NSString* subIndent = [[NSString alloc] initWithFormat:@"%@%@", 
                           indent, ([indent length]/2)%2==0 ? @"| " : @": "];
            for (UIView* aSubview in aView.subviews) dumpView( aSubview, subIndent );
            [subIndent release];
        }
    }
}

void dumpWindows() {
    for (UIWindow* window in [UIApplication sharedApplication].windows) {
        dumpView(window, @"dumpView: ");
    }   
}
progrmr
Thanks, I will have a look. BTW, how did you produce the nice formatted dump?
Jim B
This does the trick. Found the idea in http://stackoverflow.com/questions/2528929/iphone-sdk-check-if-a-uialertview-is-showing-(BOOL)alertIsActive{ for (UIWindow* window in [UIApplication sharedApplication].windows) { if ([window.subviews count] > 0) if ([[window.subviews objectAtIndex:0] isKindOfClass:[UIAlertView class]]) return YES; } return NO;}
Jim B
A: 

Thanks for the answers. I have another post out there but I'm having trouble explaining the problem. The post is http://stackoverflow.com/questions/2715042/uialertview-dismiss-not-really-dismissed. What I need is a way of making sure that a alertview is GONE before I issue a request to put up the FBConnect session dialog. If the alert is not finished dismissing, FBConnect adds the new dialog to the same 'window' that the alert must be a subview of and when the alert goes away so does the new dialog. I am hesitate to modify FBConnect and am looking for a way to make sure the alert dismiss is completed or maybe just do a short delay to give it time. The problem persists with the code shown in the post if animated = YES, but is successful for animated = NO. This smacks of a timing problem, which I would like to eliminate.

Jim B
This should be a comment (or an edit) to your question rather than posting it as an "answer".
progrmr
+1  A: 

From the problem that you describe with windows and timing, sounds like you should implement alertView:didDismissWithButtonIndex:. You can trigger your followup code from within that method.

EDIT: if that did not work, I'd try doing a delay to execute the FB stuff after a delay when the window is really guaranteed to be gone.

Jaanus
Good idea, but I have tried that and the problem persists, even with animated = NO.
Jim B