1

按下主页按钮后,我想重新加载我的网址。我尝试了这个线程中提到的代码: How to reload a UIWebView on HomeButton press 但是一旦我从主屏幕返回应用程序,这些都不会刷新/加载我的应用程序。这是到目前为止的代码,任何想法我是什么失踪?

import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize webview;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //load url into webview
    NSString *strURL = @"http://myserver.com/firstpage";
    NSURL *url = [NSURL URLWithString:strURL];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    self.webview.delegate = self;
    [self.webview loadRequest:urlRequest];


}

- (id)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(reloadWebView:)
                                                     name:UIApplicationWillEnterForegroundNotification
                                                   object:nil];
        // Do some more stuff
    }
    return self;
}
- (void)reloadWebView:(NSNotification *)notification
{
    [webview reload];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.webview reload];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
//No Internet Connection error code
-(void)webView:(UIWebView *)myWebView didFailLoadWithError:(NSError *)error {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"You have no internet connection!" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil, nil];
    [alert show];

}
//Close app from AlertView
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    return;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

2 回答 2

1

把这段代码放进去viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadWebView:) name:UIApplicationWillEnterForegroundNotification object:nil];

这个代码在viewDidUnload

[[NSNotificationCenter defaultCenter] removeObserver:self];
于 2015-05-02T12:35:32.420 回答
1

重新加载网址

- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

或在

- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
于 2015-05-02T12:37:44.513 回答