applicationWillEnterForeground: reload Data from ViewController
Asked Answered
P

1

4

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

Thats 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's 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 that code i get two warnings:

'DataViewController' may not respond to '+refresh'

but it does work.

If i cut the line in method "applicationDidEnterBackground:" it doesn't work anymore, the app crashes. Why? How do i have to fill the applicationDidEnterBackground: method? DO i need to book some space for my application? how do I do that?

How am I able to solve these warnings? Appearently my application does not know the refresh method, but why does it work in my example above?

Thanks in advance for your help :)

Predictor answered 7/2, 2011 at 10:1 Comment(0)
C
4

Change

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

to

- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"app will enter foreground");
    [viewController refresh:NULL];
}
Coon answered 7/2, 2011 at 10:14 Comment(5)
Hum i get the same warnings :/ "Messages without a matching method signature will be assumed to return 'id' and accept' as argumentsPredictor
okay, seems to work now, no warnings anymore and the app stops crashing, thank you very much. I have to wait for the next data reload, but i think it should work now.Predictor
hum well, - (void) viewDidLoad { [self loadData]; I get the same warning in that code :xPredictor
@Dwain: Place the - (void)loadData method above the - (void)viewDidLoad method, so that the compiler knows that the function exist.Coon
Hum one question left, i tested my app directly on my iphone. In addition to that appwillenterforeground method, theres a method which makes the app reload my data, every time i hit the "refresh BUTTTON". In that appwillenterforeground method i'm calling the same method like in my "refresh button method". Why does the button work properly allthough the foreround method doesn't? It seems as if the iphone loads his data from cache, i don't know :(Predictor

© 2022 - 2024 — McMap. All rights reserved.