Can I get message when I show UIAlertView

久未见 提交于 2019-11-26 23:32:49

问题


I want to get message when system show UIAlertView so I can pause my game.

Anyone know how to figure out that?

The UIAlertView is not controlled by myself.


回答1:


Application delegate's applicationWillResignActive: will be called on interrupts. You can handle the pause there or you can even listen to the UIApplicationWillResignActiveNotification in your view controller and pause the game there.

You can look at this part of the iOS Application Guide that details the life cycle of the application and state transitions.




回答2:


A system alert is normally displayed in its own UIWindow. Install handlers for the UIWindowDidBecomeVisibleNotification and UIWindowDidBecomeHiddenNotification notifications to track when a UIWindow becomes visible and hidden respectively:

 [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(aWindowBecameVisible:)
                                              name:UIWindowDidBecomeVisibleNotification
                                            object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(aWindowBecameHidden:)
                                              name:UIWindowDidBecomeHiddenNotification
                                            object:nil];

In the handlers, grab the UIWindow that changes state from the object property of the notification:

- (void)aWindowBecameVisible:(NSNotification *)notification
{
    UIWindow *theWindow = [notification object];
    NSLog(@"Window just shown: %@", theWindow);
}

- (void)aWindowBecameHidden:(NSNotification *)notification
{
    UIWindow *theWindow = [notification object];
    NSLog(@"Window just hidden: %@", theWindow);
}

Finally, check that theWindow contains a subview of type UIAlertView.




回答3:


If your UIAlertView is from Third party app (not from your app) then you can implement below delegate methods to pause and resume game.

To Pause game

- (void)applicationWillResignActive:(UIApplication *)application {
}

To Resume game

- (void)applicationDidBecomeActive:(UIApplication *)application {
}

For example if you receive call or SMS you can use above delegate to pause/resume game.




回答4:


Just make this:

- (void)applicationWillResignActive:(UIApplication *)application {
//pause
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
//resume
}


来源:https://stackoverflow.com/questions/6607170/can-i-get-message-when-i-show-uialertview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!