eatsleeptrumpet
eatsleeptrumpet

Reputation: 33

How can I add a UIActivityMonitor to my interface?

I have a view controller that houses a UIWebView. I want to display an Activity indicator in the top right so the user knows the webpage is loading; however on build&run, the activity monitor does not show.

I have a UIWebView outlet called webView and a UIActivityIndicatorView outlet called activityIndicator.

Here is my implementation:

- (void)viewDidLoad
{
    [super viewDidLoad];
    webView.delegate=self;
    [self.view addSubview:activityIndicator];
    NSString *fullURL = @"http://www.oncologyeducation.com"; NSURL *url = [NSURL URLWithString:fullURL]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [webView loadRequest:requestObj];
}

- (void)viewDidUnload
{
    [self setWebView:nil];
    [self setActivityIndicator:nil];
    [self setActivityIndicator:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
        return NO;
    } else {
        return YES;
    }
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}

-(void) webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"load started");
    [activityIndicator startAnimating];
    activityIndicator.hidden = NO;
}

-(void) webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"load finished");
    [activityIndicator stopAnimating];
    activityIndicator.hidden = YES;
}

-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [activityIndicator stopAnimating];
    activityIndicator.hidden = YES;
}
@end

On my storyboard, I have an activity indicator view which is connected to File's Owner. Thank you for any and all advice!

Upvotes: 0

Views: 469

Answers (1)

eatsleeptrumpet
eatsleeptrumpet

Reputation: 33

@CodaFi - you're right, that was not necessary. No need to add the subview.

Anybody who wants to reuse the code, lose the line:

[self.view addSubview:activityIndicator]; 

Turns out xCode was acting kind of wonky and copying the wrong storyboard into the project on compile. I renamed my storyboard, set it to the new name in the project settings, and the indicator spun with glee!

Upvotes: 0

Related Questions