applicationWillEnterForeground:从ViewController重新加载数据 [英] applicationWillEnterForeground: reload Data from ViewController

查看:335
本文介绍了applicationWillEnterForeground:从ViewController重新加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用程序从后台切换到前台后立即重新加载数据.

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

那是我的代码:

DataAppDelegate.h

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

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

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

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"可能不响应"+刷新"

'DataViewController' may not respond to '+refresh'

但它确实有效.

如果我在方法"applicationDidEnterBackground:"中剪断该行,它将不再起作用,则该应用程序将崩溃. 为什么? 我该如何填写applicationDidEnterBackground:方法? 我需要为我的应用程序预订一些空间吗?我该怎么办?

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 :)

推荐答案

更改

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

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

这篇关于applicationWillEnterForeground:从ViewController重新加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆