How to prevent UIwebView refreshing?

时光总嘲笑我的痴心妄想 提交于 2019-12-02 10:04:46

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.

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