我希望我的应用在应用从后台切换到前台后立即重新加载数据。
这是我的代码:
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'可能无法响应'+ refresh'
但确实有效。
如果我在方法“applicationDidEnterBackground:”中删除该行,它将不再起作用,应用程序崩溃。 为什么? 我如何填写applicationDidEnterBackground:方法? 我需要为我的申请预订一些空间吗?我该怎么做?
我如何解决这些警告? 显然我的应用程序不知道刷新方法,但为什么它在上面的例子中有用?
提前感谢您的帮助:)
答案 0 :(得分:4)
更改
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(@"app will enter foreground");
[DataViewController refresh];
}
到
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(@"app will enter foreground");
[viewController refresh:NULL];
}