views:

327

answers:

3

I am new to iphone development.I want my activity indicator to be displayed until the full web page is loaded.Please help me out with some sample programs or blogs.Thanks

 webView = [[UIWebView alloc] initWithFrame:webFrame];  
 webView.backgroundColor = [UIColor greenColor]; 
 [second.view addSubview:webView];

 webView.backgroundColor = [UIColor grayColor];
 webView.scalesPageToFit=YES;
 [webView release];
 NSString *urlAddress = storyLink;
 NSURL *url = [NSURL URLWithString:urlAddress];
 NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
 [webView loadRequest:requestObj];
 [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView startAnimating];

I have used the above code inside

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {}

method.

I have created the activity indicator in the following method

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [super viewWillAppear:YES];

    UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc]  initWithFrame:CGRectMake(0, 0, 20, 20)];
    UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
    [self navigationItem].rightBarButtonItem = barButton;   
}

For me activity idicator is created and destroyed within 3 seconds.I want it to be displayed till webpage is fully loaded.Please help me out.Thanks.

A: 

Please show us the code of the activity you are performing (loading the webpage). The UI updates only when your code returns control to the run loop. If your code blocks the main thread (e.g. with a synchronous NSURLConnection), the activity indicator won't start spinning until your code has completed. You have to do your work in a second thread or use asynchronous operations.

Ole Begemann
please check i a have edited my code
Warrior
+1  A: 

First, don't call [super viewWillAppear:] twice. Just call it once. Second, you're leaking activityIndicator and barButton.

You haven't indicated where you've put the stopAnimating call. To do what you've suggested, you'll need to set yourself as the delegate for the webView, and stop the animation in webViewDidFinishLoad:.

Rob Napier
+1  A: 

You need to implement the UIWebViewDelegate protocol in order to get informed about the loading state of your web page in the UIWebView:

- (void)webViewDidStartLoad:(UIWebView *)webView {
  //start animating your activity indicator here.
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
  //stop animating your activity indicator here.
}

Based on those callbacks invokations, you can start / stop your activity indicator.

yonel