gpt4 book ai didi

iphone - 如何在 UIWebView 加载时实现 UIActivityIndi​​catorView? (iPhone 对象)

转载 作者:太空狗 更新时间:2023-10-30 03:27:54 24 4
gpt4 key购买 nike

我想知道如何在基于 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");
}

最佳答案

为什么要在 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 中的 socket ),但是在它被初始化之后您将引用设置为 nil。删除该行。然后返回界面生成器并设置“停止时隐藏”复选框。现在您可以简化显示指标的代码:

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

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

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

关于iphone - 如何在 UIWebView 加载时实现 UIActivityIndi​​catorView? (iPhone 对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1311088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com