ApplicationWillEnterForeground: reload data from ViewController

I want my application to reload data as soon as the application switches from background to foreground.

Here is my code:

DataAppDelegate.h

#import <UIKit/UIKit.h> #import "DataViewController.h" @class DataViewController; @interface DataAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; DataViewController *viewController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet DataViewController *viewController; @end 

AppDelegate.m

 #import "DataAppDelegate.h" #import "DataViewController.h" @implementation DataAppDelegate @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 view to the window and display. [self.window addSubview:viewController.view]; [self.window makeKeyAndVisible]; return YES; } - (void)applicationWillResignActive:(UIApplication *)application { NSLog(@"app will resign active"); } - (void)applicationDidEnterBackground:(UIApplication *)application { NSLog(@"app did enter background"); [DataViewController refresh]; } - (void)applicationWillEnterForeground:(UIApplication *)application { NSLog(@"app will enter foreground"); [DataViewController refresh]; } 

DataViewController.h

  @interface DataViewController : UIViewController<UIWebViewDelegate, ADBannerViewDelegate> { IBOutlet UIWebView *webView; IBOutlet UITextField *addressBar; IBOutlet UIActivityIndicatorView *activityIndicator; IBOutlet UILabel *myField2; IBOutlet UILabel *myField3; IBOutlet UILabel *myField4; IBOutlet UILabel *myField5; IBOutlet UILabel *myField6; ADBannerView *adView; BOOL bannerIsVisible; } @property(nonatomic,retain) UIWebView *webView; @property(nonatomic,retain) UITextField *addressBar; @property(nonatomic,retain) UIActivityIndicatorView *activityIndicator; @property(nonatomic,assign) BOOL bannerIsVisible; -(IBAction) goBack:(id)sender; -(IBAction) goForward:(id)sender; -(IBAction) refresh:(id)sender; -(IBAction) goImpressum:(id)sender; @end 

DataViewController.m

 - (void) viewDidLoad { [self loadData]; } - (void)loadData { // contains information the ViewController makes use of } -(IBAction)refresh:(id) sender { [self loadData]; } 

With this code, I get two warnings:

'DataViewController' may not respond to '+ refresh'

but it works.

If I cut a line in the method "applicationDidEnterBackground:" it no longer works, the application crashes. What for? How do I populate the applicationDidEnterBackground method :? Do I need to reserve a place for my application? how to do it?

How can I resolve these warnings? Apparently, my application does not know the update method, but why does it work in my example above?

Thanks in advance for your help :)

+4
objective-c multitasking
source share
1 answer

Edit

  - (void)applicationWillEnterForeground:(UIApplication *)application { NSLog(@"app will enter foreground"); [DataViewController refresh]; } 

to

 - (void)applicationWillEnterForeground:(UIApplication *)application { NSLog(@"app will enter foreground"); [viewController refresh:NULL]; } 
+4
source share

All Articles