applicationWillEnterForeground + reload

macrumors newbie

My app uses 5 arrays and a webview content.
I would like to reload these contents as soon as the app is called from the background.
I'm still using a refresh button, it reloads all the information i need.
But now i want to let the app reload all information automatically.
I suppose, that i have to set up the "applicationwillenterforeground" method, but i don't really know how to do it.

testAppDelegate.m
#import "testAppDelegate.h"
#import "testViewController.h"
@implementation testAppDelegate
@synthesize window;
@synthesize viewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end

Moderator

Staff Member

I suppose, that i have to set up the "applicationwillenterforeground" method, but i don't really know how to do it.

Click to expand...

Yes, but you will need to use the applicationWillEnterForeground: method that resides in your app delegate and not within your view controller. Inside it, you should call your view controller's refresh: method.

Moderator

Staff Member

testAppDelegate doesn't know about webView. But it does know about an instance of testViewController, for which you've publicly declared a refresh: instance method.

P.S. The common convention is to name classes starting with upper-case, so as to not confuse them with variables, which normally start with lower-case. So, I would suggest testAppDelegate become TestAppDelegate and testViewController become TestViewController.

Your error is caused by trying to call a refresh class method, that is not defined, on the class TestViewController.

As for the upper-case-starting of TestViewController, perhaps I wasn't clear: you can't simply change it where you use it; you need to refactor the entire class if you want to name it TestViewController instead of the old testViewController.

Do you know what an instance variable is, and how you define the class type for it? If you answer no to either of these questions, I'd suggest it's time to step back from the real coding and go (re)learn the fundamentals of Objective-C programming. Understanding the basics is key to learning to walk before you run.

P.S. Please don't be careless with the case of your variable/class names. Objective-C is case-sensitive and TestViewController, testViewController, and Testviewcontroller are all different things in its eyes. Yes, you may just be putting these into a forum post and not in actual code but it can be confusing to us readers as well when you're not accurate.

thread startermacrumors newbie

Yeah i know that objective-c is case sensitive like Java.
I renamed these files before I posted them in here, but i didn't take care of it, sorry.
But there is another problem, i still have that warning, that the TestViewController may not respond to refresh method.

Well, but actually it does work, at least a part of it.

I switch the application to the background.
I call it to the foreground, but it instantly switches to the background again.
As soon as i call it from the background again, it refreshes the content.

But i don't really want it to switch to the background without any orders.

Any ideas why this happens?

Mousover shows some additional information:

(Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments)

macrumors 68030

It's possible for a view controller, or any object, to register to recieve the UIApplicationWillEnterForegroundNotification notification. Sometimes that makes more sense than depending on the app delegate callback.

I got another problem, my app recieves a string and splits the string into arrays. These arrays are used to fill several labels.
Today I had the problem, that my string was incomplete, the server made some mistakes, actually there was no input for one part of the string.
My app wasn't able to seperate the string any more and my app broke down for these 20 minutes.
My code:

then you are still going to get a warning. You should go back and review my post from earlier. If you want to call the refresh: instance method for TestViewController, first you must give it an object instance of TestViewController (TestViewController is a class not an object) and second, you should call the properly-named method (there's a difference between refresh and refresh:).

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.