2014-01-21 122 views
-1

我在xcode中5.開發用於iOS7應用我有一個視圖控制器ADMSViewController .h文件是如下如何在UIWebView上實現進度條?

#import <UIKit/UIKit.h> 
#import "IMTWebView.h" 
@interface ADMSViewController : UIViewController <UIWebViewDelegate,IMTWebViewProgressDelegate> 
{ 
    NSString *barcode; 
    NSString *vinNumber; 
    NSTimer *timer; 

} 
@property (nonatomic,retain) IBOutlet UIWebView *webView; 
@property (nonatomic,retain) IBOutlet UIProgressView *progressView; 
@end 

.m文件是如下

#import "ADMSViewController.h" 

#import <QuartzCore/QuartzCore.h> 


@implementation ADMSViewController 

@synthesize webView; 
@synthesize progressView; 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view, typically from a nib. 
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com/"]]]; 

} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    [self.navigationController setNavigationBarHidden:YES animated:animated]; 

} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
    [self.navigationController setNavigationBarHidden:NO animated:animated]; 

} 

- (void)webView:(IMTWebView *)_webView didReceiveResourceNumber:(int)resourceNumber totalResources:(int)totalResources { 
    [self.progressView setProgress:((float)resourceNumber)/((float)totalResources)]; 
    if (resourceNumber == totalResources) { 
     _webView.resourceCount = 0; 
     _webView.resourceCompletedCount = 0; 
    } 
} 


- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    self.webView = nil; 
    self.progressView = nil; 
} 

@end 

的IMTWebview。 h文件是如下

#import <UIKit/UIKit.h> 

@class IMTWebView; 

@protocol IMTWebViewProgressDelegate <NSObject> 
@required 
- (void) webView:(IMTWebView*)webView didReceiveResourceNumber:(int)resourceNumber totalResources:(int)totalResources; 
@end 

@interface IMTWebView : UIWebView { 
} 

@property (nonatomic, assign) int resourceCount; 
@property (nonatomic, assign) int resourceCompletedCount; 

@property (nonatomic, assign) id<IMTWebViewProgressDelegate> progressDelegate; 

@end 

的IMTWebCiew.m文件是如下

#import "IMTWebView.h" 

@interface UIWebView() 
-(id)webView:(id)view identifierForInitialRequest:(id)initialRequest fromDataSource:(id)dataSource; 
-(void)webView:(id)view resource:(id)resource didFinishLoadingFromDataSource:(id)dataSource; 
-(void)webView:(id)view resource:(id)resource didFailLoadingWithError:(id)error fromDataSource:(id)dataSource; 
@end 

@implementation IMTWebView 

@synthesize progressDelegate; 
@synthesize resourceCount; 
@synthesize resourceCompletedCount; 

-(id)webView:(id)view identifierForInitialRequest:(id)initialRequest fromDataSource:(id)dataSource 
{ 
    [super webView:view identifierForInitialRequest:initialRequest fromDataSource:dataSource]; 
    return [NSNumber numberWithInt:resourceCount++]; 
} 

- (void)webView:(id)view resource:(id)resource didFailLoadingWithError:(id)error fromDataSource:(id)dataSource { 
    [super webView:view resource:resource didFailLoadingWithError:error fromDataSource:dataSource]; 
    resourceCompletedCount++; 
    if ([self.progressDelegate respondsToSelector:@selector(webView:didReceiveResourceNumber:totalResources:)]) { 
     [self.progressDelegate webView:self didReceiveResourceNumber:resourceCompletedCount totalResources:resourceCount]; 
    } 
} 

-(void)webView:(id)view resource:(id)resource didFinishLoadingFromDataSource:(id)dataSource 
{ 
    [super webView:view resource:resource didFinishLoadingFromDataSource:dataSource]; 
    resourceCompletedCount++; 
    if ([self.progressDelegate respondsToSelector:@selector(webView:didReceiveResourceNumber:totalResources:)]) { 
     [self.progressDelegate webView:self didReceiveResourceNumber:resourceCompletedCount totalResources:resourceCount]; 
    } 
} 

@end 

我的問題是,在web視圖中進度條不是動畫。請幫助我,因爲我是ios開發的新手。如果還有其他方法可以實施,請通知我。 預先感謝您的回答。

+0

你有沒有使用記錄或斷點以確保'資源:didFinishLoadingFromDataSource:'正在像您期望的叫什麼? – matt

+0

沒有它的未被調用。 – user2541457

+0

鏈接您的樣本項目 – codercat

回答

0

對於這種事情,用的Content-Length頭,你真的只能「發現」頁面大小,但除此之外,你仍然可以呈現負載控制(如移動斑馬線類型的酒吧不顯示的一個一個離散的百分比進度,但顯示活動正在發生..甚至只是一個UIActivityIndi​​cator)。你可以嘗試像http://allseeing-i.com/ASIHTTPRequest/ASIWebPageRequest一個現成的課呢,它可以顯示進度。作者指出:

ASIWebPageRequests目前不支持進步提供了外部資源的整個請求跟蹤。然而,從進度外部資源的請求更新傳遞到您的委託,所以有一些工作有可能要自己實現。