如何防止UIwebView刷新? [英] How to prevent UIwebView refreshing?

查看:107
本文介绍了如何防止UIwebView刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个应用程序,其中包含许多在UIwebview中加载的PDF文件。每当我打开PDF,关闭它,然后再回到它,文件再次从顶部开始,它重新加载。一旦用户退出文件并回到它,我不希望它重新加载。

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.

我的代码示例如下。

(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];
}


推荐答案

实际上,我不是认为 UIWebView 是令人耳目一新的。这将假设相同的 UIWebView 。由于您发布了 -viewDidLoad 代码,实际发生的是您正在创建一个全新的 UIViewController ,它的所有属性也是新创建的。

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.

我能想到的唯一方法就是让属性到 UIViewController ,这样当你把它从屏幕上弹出时它就不会被销毁(我假设你正在关闭它将它从 UINavigationController 堆栈的顶部弹出。这可以解释为什么你的 viewDidLoad 代码多次运行,因为每个 UIViewController 实例只运行一次。

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.

因此,在您的 AppDelegate (或其他应用级单例对象)中,创建一个如下属性:

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

@property (strong, nonatomic) UIViewController *pdfViewController;

然后,当需要加载PDF以供查看时,您可以查看此属性是否已经存在,如果是这样,只需按下已经在内存中的 UIViewController 即可。这应该保持用户在弹出 UIViewController 时看到的相同视图。我认为这样的东西会起作用(假设这段代码在 IBAction 或其他东西):

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:每个实例调用一次

  • -viewWillAppear:每次按下/显示视图控制器时调用

  • -viewDidAppear:每次按下/显示视图控制器时调用

当然,如果你想有选择地显示一个 多个 PDF文件中,您还需要具有描述当前正在显示哪个PDF的属性。是否为每个PDF文件创建不同的 UIViewController 实例(并保留属性)取决于您。这绝对是您具体问题的问题。在同时加载几个单独的实例后,此方法可能会导致内存问题。

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.

这篇关于如何防止UIwebView刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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