0

我正在尝试加载 UIWebview 并尝试在加载 webview 时实现 SVProgress HUD,然后在加载时将其关闭。我几乎完成了它但是当它加载进度 hud 显示一秒钟然后再显示然后显示一秒钟和第四次,就像它闪烁我有点卡住了,看看我的代码。

在我的 .h 中:

#import <UIKit/UIKit.h>
#import "SVProgressHUD.h"


@interface ViewController2 : UIViewController <UIWebViewDelegate> {

//Instantiated the timer varibale inside the interface.
NSTimer *timer1;
IBOutlet UIWebView *webView;


}

//Instantiate the webview ans NSString link.
@property(nonatomic,retain) UIWebView *webView;
@property (strong, nonatomic) NSString *link;

在我的.m

#import "ViewController2.h"

@interface ViewController2 ()

@end

@implementation ViewController2

@synthesize webView;

- (void)viewDidLoad
{
[super viewDidLoad];

//Getting the link from the NSString called "link" and putting in the request.


NSString *link = [NSString stringWithString:self.link];

    NSURL *URL = [NSURL URLWithString:link];

    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    [webView loadRequest:request];


    //Set the time for the loagind modal view.

    timer1 = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self    selector:@selector(loading) userInfo:nil repeats:YES];




    //Webview Properties
    //Webview's name is web, which was instantianted in the propeties section.

    webView.ScalesPageToFit = true;
    webView.backgroundColor = [UIColor pomegranateColor];


    }

 //When the UIWebview is loading present "Fetching Homework" alert, thanks to SVProgressHUD

-(void)loading {
if(!webView.loading)
    [SVProgressHUD dismiss];
else
    [SVProgressHUD showWithStatus:@"Loading Homework"];
}

我认为它可能只是计时器,但我不确定,任何帮助将不胜感激。

提前致谢。

4

1 回答 1

1

您应该使用 Web 视图委托来了解页面何时结束加载,而不是使用计时器每 0.5 秒检查一次。试试这种方式:

初始化页面加载时开始 SVProgressHUD

     [SVProgressHUD showWithStatus:@"Loading Homework"];  

然后在页面加载完成后将其删除。

  - (void)webViewDidFinishLoad:(UIWebView *)webView {
        [SVProgressHUD dismiss];
    }

不要忘记您还必须将您的 webview 委托设置为:

     webView.delegate = self;
于 2013-08-21T22:17:39.987 回答