当前位置:  开发笔记 > 前端 > 正文

在UIWebView加载时如何实现UIActivityIndi​​catorView?(iPhone ObjC)

如何解决《在UIWebView加载时如何实现UIActivityIndi​​catorView?(iPhoneObjC)》经验,为你挑选了1个好方法。

我想知道如何在基于WebView的应用程序中实现activityIndi​​cator,我写了下面的代码,但指标没有出现.

webview在本地加载文件,因此加载速度非常快,但是当它加载外部页面时加载速度很慢,我需要指标...

FirstViewController.h

 #import 

 @interface FirstViewController : 
 UIViewController {
    IBOutlet UIWebView *webview1;   
    NSURL *urlLocation;     
    IBOutlet UIActivityIndicatorView *m_activity; 
 }

 @property (nonatomic, retain) UIActivityIndicatorView *m_activity;

 - (IBAction)searchbutton:(id)sender;
 - (IBAction)home:(id)sender;

 @end

FirstViewController.m

 #import "FirstViewController.h"

 @implementation FirstViewController


 @synthesize m_activity;

 // viewWillAppear loads every time younopen up this View

 - (void)viewWillAppear:(BOOL)animated {
   NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];          
   urlLocation = [NSURL fileURLWithPath:filePath];  
   [webview1 loadRequest:[NSURLRequest requestWithURL:urlLocation]]; 
 }




 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     
  if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {      
     //Initialization code      
     m_activity = nil;  
   }    
   return self; 
 }

 - (void)webViewDidFinishLoad:(UIWebView *)webView {    
   m_activity.hidden= TRUE;     
   [m_activity stopAnimating];  
   NSLog(@"Web View started loading...");
 }

 - (void)webViewDidStartLoad:(UIWebView *)webView {     
   m_activity.hidden= FALSE;    
   [m_activity startAnimating];     
   NSLog(@"Web View Did finish loading");
 }

Matt Long.. 19

为什么要在init中将活动指示器设置为nil?

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     
  if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {      
     //Initialization code      
     m_activity = nil;  
   }    
   return self; 
}

对super的调用从您的XIB初始化您的指示器(假设您将它连接到IB中的插座),但是在初始化之后您将引用设置为nil.删除该行.然后返回界面构建器并设置"隐藏时隐藏"复选框.现在,您可以简化显示指标的代码:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
   [m_activity stopAnimating];  
}

- (void)webViewDidStartLoad:(UIWebView *)webView {     
   [m_activity startAnimating];     
}

"停止时隐藏"会导致指示器在停止动画时隐藏.



1> Matt Long..:

为什么要在init中将活动指示器设置为nil?

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     
  if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {      
     //Initialization code      
     m_activity = nil;  
   }    
   return self; 
}

对super的调用从您的XIB初始化您的指示器(假设您将它连接到IB中的插座),但是在初始化之后您将引用设置为nil.删除该行.然后返回界面构建器并设置"隐藏时隐藏"复选框.现在,您可以简化显示指标的代码:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
   [m_activity stopAnimating];  
}

- (void)webViewDidStartLoad:(UIWebView *)webView {     
   [m_activity startAnimating];     
}

"停止时隐藏"会导致指示器在停止动画时隐藏.


你不需要在`didFailLoadWithError`方法中关闭它吗?
推荐阅读
有风吹过best
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有