applicationWillEnterForeground:从ViewController重新加载数据

问题描述:

我希望我的应用程序在应用程序从背景切换到前景时立即重新加载数据。applicationWillEnterForeground:从ViewController重新加载数据

那是我的代码:

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]; 
    } 

与该代码我得到两个警告:

'DataViewController' 可能不是 '+刷新'

回应,但它确实工作。

如果我在方法“applicationDidEnterBackground:”中删除了该行,它就不再工作,应用程序崩溃。 为什么? 我该如何填写applicationDidEnterBackground:方法? 我需要为我的应用程序预订一些空间吗?我怎么做?

我该如何解决这些警告? 显然我的应用程序不知道刷新方法,但为什么它在我上面的示例中工作?

在此先感谢您的帮助:)

变化

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

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

呜呜,我得到同样的警告:/“消息不匹配的方法签名将被假定为返回'ID'并接受'作为参数 – Dwain 2011-02-07 10:23:37