How to prevent UIwebView refreshing?

孤街浪徒 提交于 2019-12-31 07:02:04

问题


I have made an app that contains a lot of PDF files that are loaded in the UIwebview. Whenever I open the PDF, close it, and then go back to it, the file starts from the top again, it reloads. I don't want it to reload once the user goes out of the file and comes back to it.

Example of my code is below.

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

        NSString *path = [[NSBundle mainBundle] pathForResource:@"HnonC" ofType:@"pdf"];
        NSURL *url = [NSURL fileURLWithPath:path];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [Webview loadRequest:request];
        [Webview setScalesPageToFit:YES];

        [super viewDidLoad];
}

回答1:


Actually, I don't think the UIWebView is "refreshing". That would assume the same UIWebView. Since you're posting your -viewDidLoad code, what is actually happening is that you're creating a whole new UIViewController, with all of its properties also being newly created.

The only way I can think of to keep this from happening is to make a strong property to the UIViewController so that it isn't destroyed when you pop it off the screen (I'm assuming you're "closing" it by popping it off the top of a UINavigationController stack). That would explain why your viewDidLoad code is running multiple times, since that will run just once per UIViewController instance.

So, in your AppDelegate (or another app-level singleton object), make a property like this:

@property (strong, nonatomic) UIViewController *pdfViewController;

Then, when it's time to load the PDF for viewing, you can check to see if this property already exists and, if so, simply push the UIViewController that is already in memory. That should maintain the same view that the user was seeing when the UIViewController was popped. I would think something like this would work (assuming this code is in an IBAction or something):

AppDelegate *del = [[UIApplication sharedApplication] delegate];
UIViewController *pdfViewController = del.pdfViewController;
if (pdfViewController == nil) {
    pdfViewController = // Some code to create the view controller
    del.pdfViewController = pdfViewController;
} 
[self.navigationController pushViewController:pdfViewController];

You can pick what happens by selectively overriding certain view lifecycle methods:

  • -viewDidLoad: called once per instance
  • -viewWillAppear: called each time view controller is pushed/shown
  • -viewDidAppear: called each time view controller is pushed/shown

Of course, if you wish to selectively show one of multiple PDF files, you will need to also have a property describing which PDF is currently being shown. Whether you make a different UIViewController instance (and keep a property to it) per PDF file is up to you. That's definitely a question of your specific problem. This approach will likely cause memory issues after only a few separate instances are loaded simultaneously.



来源:https://stackoverflow.com/questions/22276886/how-to-prevent-uiwebview-refreshing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!