Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RagingGoat

macrumors 6502
Original poster
Jun 21, 2010
307
15
My app has a table view that gets its data from an RSS feed. When a row is selected, a web view is shown with the article for the row selected. I have my activity indicator but it never stops and therefore doesn't hide.

Here is the didSelectRowFromIndexPath method from the table view.

Code:
- (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;
}

Here is my web view controller.
Code:
#import "WebViewController.h"
#import "TUSafariActivity.h"
#import "SVProgressHUD.h"

@implementation WebViewController
{

}

@synthesize webView=webView, hackyURL=hackyURL, activityIndicator;

- (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.
    
    CGFloat width = CGRectGetWidth(self.view.bounds);
    CGFloat height = CGRectGetHeight(self.view.bounds);
    
    // start an animator symbol for the webpage loading to follow
	UIActivityIndicatorView *progressWheel = [[UIActivityIndicatorView alloc]
    initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    
	// makes activity indicator disappear when it is stopped
	progressWheel.hidesWhenStopped = YES;
	
    // used to locate position of activity indicator
	progressWheel.center = CGPointMake(width / 2, height / 2);
    
	self.activityIndicator = progressWheel;
	[self.view addSubview: self.activityIndicator];
    self.activityIndicator.hidesWhenStopped = YES;
	[self.activityIndicator startAnimating];
    
    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)webViewDidStartLoad:(UIWebView *)wv
{
    [self.activityIndicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)wv
{     
    [self.activityIndicator stopAnimating];
}

@end
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.