2011-02-07 64 views
4

我希望我的應用程序在應用程序從背景切換到前景時立即重新加載數據。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:方法? 我需要爲我的應用程序預訂一些空間嗎?我怎麼做?

我該如何解決這些警告? 顯然我的應用程序不知道刷新方法,但爲什麼它在我上面的示例中工作?

在此先感謝您的幫助:)

回答

4

變化

- (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

相關問題