【问题标题】:How implement a UIActivityIndicatorView when the UIWebView is Loading? (iPhone ObjC)UIWebView 加载时如何实现 UIActivityIndi​​catorView? (iPhone 对象)
【发布时间】:2009-08-21 10:07:56
【问题描述】:

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

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

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");
 }

【问题讨论】:

    标签: iphone objective-c xcode activity-indicator


    【解决方案1】:

    为什么在初始化中将活动指示器设置为 nil?

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

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

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

    “停止时隐藏”会导致指示器在您停止动画时隐藏。

    【讨论】:

    • 你不是也需要在didFailLoadWithError方法中关闭它吗?
    【解决方案2】:

    这里有什么问题,您在上面发布的代码应该可以工作,除了您没有在任何地方初始化指标(也许您在 viewDidLoad 中进行)但上面显示的代码应该可以工作,因为指标已正确初始化并且您设置了 webview d 提升到那里的视图控制器,我让它在我的一些应用程序上工作,我使用 webviews 和指示器来指示它何时加载......

    【讨论】:

      【解决方案3】:

      也可以使用 UIWebView.loading 属性。

      Apple 的文档: @property(nonatomic, readonly, getter=isLoading) BOOL 加载 描述 一个布尔值,指示接收器是否已完成加载内容。 (只读) 如果是,接收方仍在加载内容;否则,否。

      在 iOS6 中,Apple 似乎也修复了此属性的一些问题。 http://code-gotcha.blogspot.fi/2012/08/uiwebviewloading-in-ios-6-fixed.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多