如何在UIWebView加载时实现UIActivityIndi​​catorView? (iPhone ObjC) [英] How implement a UIActivityIndicatorView when the UIWebView is Loading? (iPhone ObjC)

查看:101
本文介绍了如何在UIWebView加载时实现UIActivityIndi​​catorView? (iPhone ObjC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在基于WebView的应用程序中实现activityIndi​​cator,我编写了以下代码,但指标未出现。

I want to know how to implement an activityIndicator in a WebView based app, I wrote the following code but the indicator does not appear.

本地webview加载文件,所以加载速度非常快,但加载外部页面时加载速度很慢,我需要指标......

The webview load file locally, so it load very fast, but when it load an external page it load slow and I need the indicator...

FirstViewController.h

 #import <UIKit/UIKit.h>

 @interface FirstViewController : 
 UIViewController <UIWebViewDelegate>{
    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");
 }


推荐答案

为什么要设置活动init中的指示符为nil?

Why are you setting your activity indicator to nil in your init?

- (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。删除该行。然后返回界面构建器并设置隐藏时隐藏复选框。现在您可以简化显示指标的代码:

The call to super initialized your indicator from your XIB (assuming you connected it to your outlet in IB), but then you are setting the reference to nil after it's been initialized. Remove that line. Then go back into interface builder and set the "Hide when stopped" checkbox. Now you can simplify your code that displays the indicator:

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

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

停止时隐藏会导致指示符在您停止时隐藏动画。

The "Hide when stopped" causes the indicator to hide when you stop it from animating.

这篇关于如何在UIWebView加载时实现UIActivityIndi​​catorView? (iPhone ObjC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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