applicationWillEnterForeground:从 ViewController 重新加载数据

发布于 2024-10-15 23:46:35 字数 2875 浏览 3 评论 0原文

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

这是我的代码:

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: 方法? 我需要为我的申请预订一些空间吗?我该怎么做?

我怎样才能解决这些警告? 显然我的应用程序不知道刷新方法,但为什么它在上面的示例中有效?

预先感谢您的帮助:)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是一片海 2024-10-22 23:46:35

更改

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

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

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文