In my app, When I press the home button, I pop the application to rootviewcontroller login class. Now, in the previous class, if any alertview comes and without dismissing it I press home button, Problem appears. Next time I tap on app icon to bring it in fore ground, the login screen is there, but on top of it, previous class alertview is also there. How to remove the alertviews on applicationDidEnterBackground?
Asked
Active
Viewed 750 times
0
-
duplicate of http://stackoverflow.com/questions/3105974/dismissing-uialertviews-when-entering-background-state – D_4_ni Nov 30 '12 at 13:36
2 Answers
3
Have a global (AppDelegate property or singleton) where you simply store the pointer to the last alert view displayed (and clear it when done). If the pointer is non-nil, dismiss it in DidEnterBackground or wherever.

Hot Licks
- 47,103
- 17
- 93
- 151
-
-
1@D_4_ni Why not? You never show more than one alert at a time so the ivar is always referencing the current one (if any). – rmaddy Nov 30 '12 at 15:46
-
@rmaddy Yes, you can show more than one at a time - just one is _visible_ at the same time. If an alert is visible and another one pops up, the latter will be made visible. When you dismiss the second alert, the first one is still there. – D_4_ni Dec 01 '12 at 19:12
-
@D_4_ni True. Though I didn't say you can't. I've never found any reason for more than one at a time. As long as you never do (like me), then Hot Lick's answer works. – rmaddy Dec 01 '12 at 19:18
-
@rmaddy You often experience this with network errors - take Apple's Mail app as an example. When you have multiple mail accounts set up, and you are not connected to any network with internet access, you will get multiple alerts. You can find a way that works with multiple alerts in the question I linked in my comment on this question (although I didn't test it, it should work) – D_4_ni Dec 01 '12 at 19:24
1
You can also use UIApplicationWillResignActiveNotification,UIApplicationWillEnterForegroundNotification to solve this issue
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(resignActive)
name:UIApplicationWillResignActiveNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(enterForeground)
name:UIApplicationWillEnterForegroundNotification
object:nil];
In resignActive method you can remove the alertviews...

Murali
- 1,869
- 1
- 14
- 22