viewDidAppear isn't getting called after running from the background - iphone

Method viewDidAppear is not getting called after going from the background back into my iPhone Application.
Does anyone have a solution for this?
I need to call a method every time my MainViewController is shown (even after returning from the background) that will change a label to the new date.
viewDidAppear doesn't seem to be working properly.

Actually it is working properly.
Instead, consider installing a notification handler for UIApplicationDidBecomeActiveNotification or UIApplicationWillEnterForegroundNotification, whichever may be more appropriate for your situation. From there you could do an update of your GUI.

You will have the UIApplicationWillEnterForegroundNotification method in App delegate.Now you need to create the object of the class you want and call the viewwillAppear
this method will be called when the app is in background.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
now for the view will appear code like this
- (void)applicationWillEnterForeground:(UIApplication *)application
{
ViewController *vc = [ViewController alloc]init];//you can use your viewcontroller here
[vc viewDidAppear:YES];// this will call the method.
}

Try using viewWillAppear if you dont mind doing this process before the is actually shown.
Else you can use the method.
- (void)applicationWillEnterForeground:(UIApplication *)application{}

Related

Data is not reloaded when app comes from background

Actually the requirement is to reload the data when app comes from background. but it doesn't reload when I come from background. I write the method for reloading data on viewDidLoad.
So, where should I write the code to solve my problem?
Thanks...
As KartikArora is implies above, your viewDidLoad is not called when the app comes from the background to the foreground. So the data is not reloaded.
You could reload the data whenever the view appears instead of when the view is loaded. But then it would reload the data every time the view appears, which you might not want.
You could also have a reload method in your view controller that is called when the app enters foreground triggered via a posted notification.
-(void) viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMethod:) name: UIApplicationWillEnterForegroundNotification object:nil];
}
-(void)myMethod:(id)not {
// code for save data
}
try this
Try it in App Delegate File it will work
- (void) applicationWillEnterForeground: (UIApplication *) application
{
write your code here
}
Either you do it in the UIApplicationDelegates
- (void)applicationDidBecomeActive:(UIApplication *)application
method, or you subscribe your object to the UIApplicationDidBecomeActiveNotification.

How can I ensure my UITableView data update function is called only once when app is displayed again (from background start, or launch)?

How can I ensure my table data function is called only once when app is displayed again (from background start, or launch)?
That is, I want to have my table data refreshed when the user clicks on the application icon, and only once. So the requirement would be:
a) update method in controller called only once after application icon is clicked by user
b) to be valid irrespective of whether the user is (i) starting app for the first time, (ii) coming back from background, (iii) any other means of the user requesting the app come back to the foreground.
The issue I currently have with my implementation is that the data update is being called twice in the case where the app is coming back from background, so I basically want to improve on this. The way I'm doing currently, which is flawed is:
- (void)updateEventData {
// UPDATE CODE HERE
}
- (void)becomeActive:(NSNotification *)notification {
[self updateEventData];
[self.tableView reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self updateEventData];
// Auto Refresh Data - Handle Case where App becomes active from background & you want to refresh data
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(becomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
You can implement it in following way:
1) Take one variable in NSUserDefault.
2) Set it's value FALSE when app is launch for the first time.
3) Get the value of NSUserDefault variable in respective view controller. And if it's value if FALSE then call update method.
4) Now, when app become Active, then set the variable value TRUE.
5) Get it's value in respective view controller. And if's value is TRUE then don't call update method.
6) So, update method will be called only once.
Hope it will be helpful to you. May be there is some minor changes.
Let me know in case of any difficulty.
Well what i understand from your question that you only want to update the table data when you start your application by clicking the app icon on the iPhone.Well First understand the execution of the app, the main function is called first and then in your app delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self postNotification];
[self.window makeKeyAndVisible];
return YES;
}
you can post the notification in this method as its called once.
And more over if you want to do this by your own way in the ViewDidLoad method, then i think you must also remove the observer of notification,i think that may be the reason that its being called twice,because you are not removing notification.
As part of writing this question and reviewing answers I think I've got probably a good solution...
Create an instance variable/property "initialLoad"
Set it to TRUE first thing in viewDidLoad
Then in the becomeActive method do the following:
Code:
- (void)becomeActive:(NSNotification *)notification {
if (!self.initialLoad) {
[self updateEventData];
[self.tableView reloadData];
}
self.initialLoad = false;
}
Seems to work ok...

UIViewController's viewDidAppear/viewDidDisappear: when exactly are that methods supposed to be called?

I definitely need some clarification on when exactly viewDidAppear/viewDidDisappear methods are supposed to be called...
If the application enters background while showing some view, in this case I would expect viewDidDisappear to be called on the UIViewController linked to that view. On the other hand, if the application enters foreground after being background, I would expect viewDidAppear to be called. But it doesn't work this way.
If an UINavigationController displays an UIViewController we call 'A', and that UIViewController is linked to a view that has a subview linked to another UIViewController we call 'B', the viewDidAppear method is NOT called on the controller 'B'. Do I have to propagate viewDidAppear myself? I'm confused...
Thank you in advance!
They are not called because they don't disappear and reappear unless you tell them to disappear. Your whole application is suspended. You need to listen to the app delegates applicationDidBecomeActive: and applicationWillResignActive: messages if you want to know if your app got suspended or gets reactivated. You can also register for the notifications UIApplicationDidBecomeActiveNotification and UIApplicationWillResignActiveNotification.
Yes, you have to propagate the viewDidAppear: messages to your subviews manually. This is working as designed.
There is also:
(also an app delegate method):
- (void)applicationDidEnterBackground:(UIApplication *)application

presentModalViewController calling any method in the target viewController?

When i call presentModalViewController
[self presentModalViewController:iPSPvc animated:NO];
is there a method i can implement in the target viewController (iPSPvc) that gets called every time this happens?
I need to make sure some updating of the view is done.
viewDidLoad gets called when i create an instance of iPSPvc so I need a method where I can do sometime similar.
Many Thanks
-Code
Try viewWillAppear method
-(void)viewWillAppear:(BOOL)animated
{
//your code goes here
}
what's with viewWillAppear and viewDidAppear?
- (void)viewWillAppear:(BOOL)animated
If I've understood your question aright, the above method (or one its close cousins) in your target viewController. This gets called every time the controller's view is about to appear, not just when the view is first loaded.

Multitasking and applicationWillEnterForeground

I am trying to have my app reload data when it is brought to the foreground via the applicationWillEnterForeground method. I can get the function to work and write to the NSLog, but I have a function that I want to be called from another class, how would I go about this?
I have tried below:
- (void)applicationWillEnterForeground:(UIApplication *)application {
//Re-run...
[MainViewController reRun];
}
Be gentle bit of a newbie...
You can register to UIApplicationDidBecomeActiveNotification, and reload your data when your receive it.