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];
}
Related
i want to reload table when app comes to foreground from background, in my app delegate.m i did like this, but its not working
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"applicationWillEnterForeground");
[[NSNotificationCenter defaultCenter] postNotificationName:#"EnteredForeground"
object:nil];
}
and in my viewController i am working like
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(whenAppEnteredIntoForeground:) name:#"EnteredForeground" object:nil];
}
- (void)whenAppEnteredIntoForeground:(id)object {
NSLog(#"log msg");
[tblSearch reloadData];
}
what should i do? what mistake i am doing? any help please
Firstly, you don't need to rebroadcast a notification when the app comes to the foreground, you can register for the notification from your view controller.
In your case, it's likely to be that the view is not loaded until after your secondary notification is sent, which is why your view controller cannot respond to it. Using breakpoints will confirm if this is the case.
Use this instead:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(whenAppEnteredIntoForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
And you don't need to rebroadcast a notification from your appdelegate.
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appEnteredIntoForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)appEnteredIntoForeground:(id)object {
[tableView reloadData];
}
Make sure that your Reload Data must call from main Thread Otherwise it will not be reloaded.
I am posting a notification locally in the app when ever I receive a remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"Received notification: %#", userInfo);
[[NSNotificationCenter defaultCenter] postNotificationName:#"NEWMESSAGE" object:nil userInfo:userInfo]; }
I have added an observer to the view in the function viewWillAppear() and remove the observer in viewWillDisappear().
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(newMessageReceived:) name:#"NEWMESSAGE" object:nil];
and
[[NSNotificationCenter defaultCenter] removeObserver:self];
My question is I want to override every viewWillAppear and viewWillDisappear functions in all *.m files that use these functions in my app.
or how can I dynamically add an observer (like above) to the present view and remove the observer when that view disappears. it should be like a global action whenever view changes observer to be added and removed when it changes again.
Is this possible? if so please guide me.
thanks in advance.
Some thoughts:
You can subclass UIViewController and implement these method in the subclass-ed view controller class. Then you need to create all your views as the subclass of this UIViewController.
Example:
//Creating a custom subclass of UIViewController
#interface CustomViewController : UIViewController
#end
#implementation CustomViewController
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(newMessageReceived:) name:#"NEWMESSAGE" object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#end
And create all your view controller as the subclass of CustomViewController.
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];
}
I want to cancel url connection when applicationDidEnterBackground is called. But I don't know how to save a connection in a whole scope of application. I created a few url connections in other view controllers, but I'd like to cancel them in AppDelegate. How can I do this?
You may add observer to UIApplicationDidEnterBackgroundNotification in your class which create the connection. Try this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(enterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
remember call this
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
in your class's dealloc
I believe your NSURLConnections will be killed when your app enters the background.
If you want to keep track of all NSURLConnections, you need to keep track of them yourself by adding them to a NSSet, NSArray, or some other datastructure, and looping through them closing them. You could also have a class that inherits or composes a NSURLConnection. This datastructure would handle adding itself to a queue, and you could clean up when the app exits.
It is better to stop the connections before app becomes inactive instead of applicationDidEnterBackground.
Answer is use notifications:
-(void)init
{
//
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(closeConnection) name:UIApplicationWillResignActiveNotification object:nil];
//
}
-(void)closeConnections
{
// [urlConnection cancel];
}
i need to dismiss my uiimagepicker modal viewcontroller automatically when the application enters the background.i tried to put the code to dismissmodalviewcontroller code in viewdiddissappear method,but its not being called.so i made a reference to the viewcontroller in appdelegate and tried to put it in the applicationdidenterbackgroundmethod but still it is not working.can someone point out the right way to do this
Try to add an NSNotificationCenter observer for UIApplicationDidEnterBackgroundNotification in the UIViewController that you want to dismiss. Use the selector to dismiss the modalview
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(didEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver: self
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
- (void)didEnterBackground:(NSNotification*)note
{
[self.navigationController dismissModalViewAnimated:NO];
}
Best way to remove the modal when app is moving to background and it works fine .
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(dismissView:)
name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)dismissView:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Also you can remove observer like this
[[NSNotificationCenter defaultCenter] removeObserver: self
name:UIApplicationDidEnterBackgroundNotification
object:nil];
I don't think you need to go through all that.
From the docs:
If you present several modal view controllers in succession, and thus build a stack of modal view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack.
Try calling [self dismissModalViewController:NO] from the parent view controller in your implementation of - (void) viewDidUnload.
This is untested, but the docs imply that it should do the job for you.