views:

70

answers:

1

In the app I am working on, I have action sheets and alert views which I would like dismissed when app enters the inactive/ background state.

I am using the UIApplicationWillResignActiveNotification instead of the UIApplicationDidEnterBackgroundNotification as I want the code to be compatible with iOS3.2.

-(void)applicationWillResignActive:(Notification *)notification{
    if (self.actionSheet && self.actionSheet.visible){
        NSLog(@" actionSheet is Visible");
        [self.actionSheet dismissWithClickedButtonIndex:0 animated:NO];
    }
}

Testing this in simulator (iphone 3.2, iOS4), with the actionSheet visible, I press the home button, but I do not get the "actionSheet is Visible" message. Yet when I re-open the app and dismiss it again with home button, I get the "actionSheet is Visible" message.

This suggests that the first time the actionSheet's visible property is not being set. Could there be a delay in the property being set? In fact I put a message in the method that displays the actionSheet

[self.actionSheet showInView:self.parentViewController.tabBarController.view];
if (self.actionSheet.Visible) NsLog(@" action Sheet visible");

even here I do not get the message. Where/ when is the visible property set? Am I doing something fundamentally wrong in trying to dismiss the actionSheet? I have seen similar very good and detailed solutions on dismissing alertViews in SO.... but they don't seem to cover this issue. Any help will be much appreciated.

+1  A: 

Why would you even need to check if it's visible? In fact, why would you even need to check it against nil? You could just put [self.actionSheet dismissWithClickedButtonIndex:0 animated:NO];, and it should work fine, as if the action sheet exists you will dismiss it, and if it doesn't, you will will just call the method on nil, which does nothing.

chpwn
Actually I saw Apple's sample code first checking if the alertView is visible and then calling the dismiss method.
prd