问题描述
我的应用程序以表格视图显示来自RSS提要的文章,然后当您选择一行时,打开一个Web视图控制器以显示该文章。我正在尝试添加加载指示器。如果选择一行,指示器将短暂显示,然后在页面完全加载之前消失。另外,如果我返回到表格视图并选择其他行,则加载指示符将永远不会显示。我正在使用一个名为SVProgressHUD的自定义加载指示器,该指示器在应用程序的其他位置都可以正常工作。我不认为这是问题所在。我在做什么错?
这是我的表格视图中的didSelectRowAtIndexPath方法:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[webViewController webView] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@ about :空白]]];
[self.navigationController pushViewController:webViewController动画:是];
//抓取所选项目
RSSItem * entry = [[频道项目] objectAtIndex:[indexPath row]];
//使用项目
的链接字符串构造URL NSURL * url = [NSURL URLWithString:[输入链接]];
//构造带有该URL的请求对象
NSURLRequest * req = [NSURLRequest requestWithURL:url];
//将请求加载到Web视图中
[[webViewController webView] loadRequest:req];
webViewController.hackyURL =网址;
}
加载指示器从WebViewController的viewDidLoad中的这一行开始:
[SVProgressHUD showWithStatus:@正在加载];
这是我的WebViewController。
#import WebViewController.h
#import TUSafariActivity.h
#import SVProgressHUD.h
@implementation WebViewController
@synthesize webView = webView,hackyURL = hackyURL;
-(void)loadView
{
//创建一个与屏幕一样大的UIWebView实例
CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
UIWebView * wv = [[UIWebView alloc] initWithFrame:screenFrame];
webView = wv;
NSLog(@%@,webView.request.URL);
//告诉Web视图缩放Web内容以适合Webview的范围
[wv setScalesPageToFit:YES];
[self setView:wv];
}
-(UIWebView *)webView
{
return(UIWebView *)[self view];
}
-(void)showMenu
{
NSURL * urlToShare = hackyURL;
NSArray * activityItems = @ [urlToShare];
TUSafariActivity * activity = [[TUSafariActivity alloc] init];
__block UIActivityViewController * activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:@ [activity]];
activityVC.excludedActivityTypes = @ [UIActivityTypeAssignToContact,UIActivityTypePostToWeibo,UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityVC动画:是完成:^ {activityVC.excludedActivityTypes = nil; activityVC = nil;}];
}
-(void)toggleBars:(UITapGestureRecognizer *)gesture
{
BOOL barsHidden = self.navigationController.navigationBar.hidden;
if(!barsHidden)
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[self hideTabBar:self.tabBarController];
}
else if(barsHidden)
{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
[self showTabBar:self.tabBarController];
}
[self.navigationController setNavigationBarHidden:!barsHidden animation:YES];
}
-(void)viewDidLoad
{
[super viewDidLoad];
//从其笔尖加载视图后,执行任何其他设置。
[SVProgressHUD showWithStatus:@正在加载];
UIBarButtonItem * systemAction = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showMenu)];
self.navigationItem.rightBarButtonItem = systemAction;
UITapGestureRecognizer * singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleBars :)];
[webView addGestureRecognizer:singleTap];
singleTap.delegate =自我;
// self.hidesBottomBarWhenPushed = YES;
}
-(布尔)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer应该可以同时识别WithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
-(void)hideTabBar:(UITabBarController *)tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
float fHeight = screenRect.size.height;
if(UIDeviceOrientationIsLandscape([UIApplication sharedApplication] .statusBarOrientation))
{
fHeight = screenRect.size.width;
}
for(UIView * tabbarcontroller.view.subviews中的视图)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x,fHeight,view.frame.size.width,view.frame.size.height)]];
}
其他
{
[view setFrame:CGRectMake(view.frame.origin.x,view.frame.origin.y,view.frame.size.width,fHeight )];
view.backgroundColor = [UIColor blackColor];
}
}
[UIView commitAnimations];
}
-(void)showTabBar:(UITabBarController *)tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
float fHeight = screenRect.size.height-49.0;
if(UIDeviceOrientationIsLandscape([UIApplication sharedApplication] .statusBarOrientation))
{
fHeight = screenRect.size.width-49.0;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView * tabbarcontroller.view.subviews中的视图)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame: CGRectMake(view.frame.origin.x,fHeight,view.frame.size.width,view.frame.size.height)];
}
其他
{
[view setFrame:CGRectMake(view.frame.origin.x,view.frame.origin.y,view.frame.size.width,fHeight )];
}
}
[UIView commitAnimations];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
//加载完成后停止活动指示器
[SVProgressHUD关闭] ;
}
@end
您应该使用网络视图
的 delegate
功能
-(void)webViewDidStartLoad:(UIWebView *)webView {
// SHOW HUD
}
-( void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
//杀死HUD
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
if(!webView.loading){
//杀死HUD
}
}
在 webview
中通常会发生缓存,因此HUD可能不会显示很长时间,因为它将在第一次加载时显示。 p>
我希望它会对您有所帮助。
My app shows articles from an RSS feed in a table view then when you select a row, opens a web view controller to show the article. I'm trying to add a loading indicator. If I select a row, the indicator will show briefly and then disappear before the page is fully loaded. Also, if I go back to the table view and select a different row, the loading indicator never shows up. I'm using a custom loading indicator called SVProgressHUD and it works fine elsewhere in the app. I don't think that is the problem though. What am I doing wrong?
Here is my didSelectRowAtIndexPath method in my table view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[webViewController webView]loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
[self.navigationController pushViewController:webViewController animated:YES];
// Grab the selected item
RSSItem *entry = [[channel items]objectAtIndex:[indexPath row]];
// Construct a URL with the link string of the item
NSURL *url = [NSURL URLWithString:[entry link]];
// Construct a request object with that URL
NSURLRequest *req = [NSURLRequest requestWithURL:url];
// Load the request into the web view
[[webViewController webView]loadRequest:req];
webViewController.hackyURL = url;
}
The loading indicator is started on this line in viewDidLoad of the WebViewController:
[SVProgressHUD showWithStatus:@"Loading"];
And here is my WebViewController.
#import "WebViewController.h"
#import "TUSafariActivity.h"
#import "SVProgressHUD.h"
@implementation WebViewController
@synthesize webView=webView, hackyURL=hackyURL;
- (void)loadView
{
// Create an instance of UIWebView as large as the screen
CGRect screenFrame = [[UIScreen mainScreen]applicationFrame];
UIWebView *wv = [[UIWebView alloc]initWithFrame:screenFrame];
webView = wv;
NSLog(@"%@",webView.request.URL);
// Tell web view to scale web content to fit within bounds of webview
[wv setScalesPageToFit:YES];
[self setView:wv];
}
- (UIWebView *)webView
{
return (UIWebView *)[self view];
}
- (void) showMenu
{
NSURL *urlToShare = hackyURL;
NSArray *activityItems = @[urlToShare];
TUSafariActivity *activity = [[TUSafariActivity alloc] init];
__block UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:@[activity]];
activityVC.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypePostToWeibo, UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityVC animated:YES completion:^{activityVC.excludedActivityTypes = nil; activityVC = nil;}];
}
- (void)toggleBars:(UITapGestureRecognizer *)gesture
{
BOOL barsHidden = self.navigationController.navigationBar.hidden;
if (!barsHidden)
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[self hideTabBar:self.tabBarController];
}
else if (barsHidden)
{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
[self showTabBar:self.tabBarController];
}
[self.navigationController setNavigationBarHidden:!barsHidden animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[SVProgressHUD showWithStatus:@"Loading"];
UIBarButtonItem *systemAction = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showMenu)];
self.navigationItem.rightBarButtonItem = systemAction;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toggleBars:)];
[webView addGestureRecognizer:singleTap];
singleTap.delegate = self;
// self.hidesBottomBarWhenPushed = YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (void) hideTabBar:(UITabBarController *) tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
float fHeight = screenRect.size.height;
if( UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
{
fHeight = screenRect.size.width;
}
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
view.backgroundColor = [UIColor blackColor];
}
}
[UIView commitAnimations];
}
- (void) showTabBar:(UITabBarController *) tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
float fHeight = screenRect.size.height - 49.0;
if( UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
{
fHeight = screenRect.size.width - 49.0;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
}
}
[UIView commitAnimations];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//stop the activity indicator when done loading
[SVProgressHUD dismiss];
}
@end
You should use the delegate
functions of a webview
see below
-(void)webViewDidStartLoad:(UIWebView *)webView{
//SHOW HUD
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
//KILL HUD
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
if(!webView.loading){
//KILL HUD
}
}
In webview
normally caching happens, so HUD might not show for long as it will show for the first load.
I hope it will help you.
这篇关于Web视图上的加载指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!