Dismiss popover when app goes in background - iphone

How to dismiss popover when application enters in background?

You can do this using the delegate method in appdelegate.m file
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//put your dissmiss popover code here
}

it is better to register your controller for UIApplicationDidEnterBackgroundNotification or UIApplicationWillResignActiveNotification and dismiss it whenever your app goes to background, this will make your life quite easier i feel.
registering for notification in your viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMethod)
name:UIApplicationDidEnterBackgroundNotification object:nil];
implement the method and
-(void)myMethod{
// dismiss popview here
}
finally un-register from the notification in your view controller
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

Send an NSNotification in your app delegate's willResignActive method, and listen for it in your view controller that contains the popup, and have it dismiss said popover when the notification is received.

try this
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//[popover dissmissPopoverAnimated:YES];
}

Related

I expect the initial view controller of my storyboard to be the current controller

If the application was previously in the background, when applicationDidBecomeActive is called, I expect the initial view controller of my storyboard to be the current controller.
I used:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[self.window makeKeyAndVisible];
}
When I restart app ,loginAciton inside rootViewController still be called ,but could not present the next controller . No errors like nothing happened.
- (IBAction)loginAciton:(id)sender
{
id controller = [self.storyboard instantiateViewControllerWithIdentifier:#"Navigation"];
[self presentModalViewController:controller animated:YES];
}
Why?
PS. My rootViewController is not a UINavigationController.
Thanks for any replies.
A much better way is to add UIApplicationExitsOnSuspend to your Info.plist and set it to YES.
I got it !
if you wanna the initial view controller of your storyboard to be the current controller every time , you can try it :
- (void)applicationDidEnterBackground:(UIApplication *)application
{
exit(EXIT_SUCCESS);
}
You can use the notification too, put this code in applicationDidBecomeActive:
[[NSNotificationCenter defaultCenter] postNotificationName:#"appActivated" object:nil];
and add observer in that current view ...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateView:)
name:#"appActivated"
object:nil];
and don't forget to remove the observer in dealloc: of your current view...
[[NSNotificationCenter defaultCenter] removeObserver:self];
may this will help you..

handle home button pressing on iphone

I need to handle home button pressing in my app.
When user presses home button in my DetailedViewController I need to trigger method that will [self.navigationController popViewControllerAnimated:YES].
Help me please.
How could it be done?
Check out - (void)applicationWillResignActive:(UIApplication *)application method in you app delegate. It will catch the event. And then your can handle it as you need. For example, post notification using default notification center and get it in class where you need to do something.
What you want exactly? you want to get back to Home(root) or just want to get back to the previous page?
just try out with this code in which you get back to your starting or home controller
[self.navigationControler popToRootViewControllerAnimated:YES];
As Павел Оганесян has described :
// post notification
- (void)applicationWillResignActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"TestNotification" object:self];
}
Now in DetailedViewController .m file
// add observer
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveTestNotification:) name:#"TestNotification"
object:nil];
}
- (void) receiveTestNotification:(NSNotification *) notification
{
// do the needful
}
Hope it helps you...

Can I get message when I show UIAlertView

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.
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.
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.
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.
Just make this:
- (void)applicationWillResignActive:(UIApplication *)application {
//pause
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
//resume
}

How to resume process on applicationDidBecomeActive

So I have around 4 viewcontrollers in this iPAd App that I am testing. Before the Application becomes inactive, the TableViewController is presnet. Once I press the button on the iPhone, it will initiate
-(void)applicationWillResignActive:(UIApplication *)application
And when I start it again, I want the application to resume with the process, with the loaded table and show the 'Screen' that was available before I pressed the Button.
I can understand that
-(void)applicationDidBecomeActive:(UIApplication *)application
is involved in this event. Could you tell me how I can actually bring a particular view controller on the event of resuming the process ??
Thanks.
You can register your own UIViewControllers as observers for `UIApplicationDidBecomeActiveNotification.
In your view controllers:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
Do not forget to remove them as observers in their dealloc methods:
- (void)dealloc {
...
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[super dealloc];
}

How to refresh UITableView after app comes becomes active again?

I would like my UITableView to reloadData once my app is active again, after a user exits the application. I know I need to implement (in my app delegate):
- (void)applicationDidBecomeActive:(UIApplication *)application
but im not sure how to reference the current UITableView?
UPDATE:
My UITableView is a separate controller. It is actually presented as follows
AppDelegate > Root View Controller > Pushes UITabBarController modally which has a UITableViewController
following up on Ole's answer above
add this when initializing the viewcontroller
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(becomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
add the actual method in the controller
- (void)becomeActive:(NSNotification *)notification {
NSLog(#"becoming active");
}
be sure to clean up the notification
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
If you can't access your view controller from the app delegate, you could also have your controller listen to the UIApplicationDidBecomeActiveNotification notification.
you can create your class called TableViewManager. in there register list of UITableView so that you can refresh any table you want.
it's like this, in yourTableViewManager class, you have a method called
- (void)RefreshTableView:(UITableView *)tableView {
if(tableView != nil)
[tableView reloadData]; }