0

I've implemented sample code using the UIWebView that I found, but it doesn't look right to me, though it works. Specifically because it sets the UIWevView delegate twice (in viewDidLoad and viewWillAppear). Also, myWebView is set as an autorelease object, but then it's released in dealoc. I'm hoping someone can tell me how to clean this up.

// *** WebViewController.h ***

@interface WebViewController : UIViewController 
<UIWebViewDelegate>
{   
    UIWebView *myWebView;
    UIActivityIndicatorView *activityIndicator;
}

@property (nonatomic, retain) UIWebView *myWebView;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;

@end

// *** WebViewController.m ***

@synthesize myWebView;

- (void) viewDidLoad {
    [super viewDidLoad];

    // - - - - -> Create the UIWebView

    CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
    webFrame.origin.y += 42.0;
    webFrame.size.height -= 106.0;

    self.myWebView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];
    self.myWebView.backgroundColor = [UIColor whiteColor];
    self.myWebView.scalesPageToFit = YES;
    self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    self.myWebView.delegate = self;
    [self.view addSubview: self.myWebView];

    // - - - - -> Create the UIActivityIndicatorView

    self.activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
    [self.view addSubview: self.activityIndicator];
    self.activityIndicator.center = CGPointMake(135,438);

    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.someURL.com/"]]];

- (void) viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
   self.myWebView.delegate = self;
}

- (void) viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
    [self.myWebView stopLoading];   
    self.myWebView.delegate = nil;  
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void) dealloc {
    myWebView.delegate = nil;
    [myWebView release];
    [activityIndicator release];
    [super dealloc];
}
Lauren Quantrell
  • 2,647
  • 6
  • 36
  • 49

1 Answers1

0

It's pretty much fine to do that. The idea is pretty basic. You do not want the delegate to receive any delegation messages when off screen. So the idea of setting the delegate -viewWillAppear: and -viewWillDisappear: is correct. But I don't see any delegate methods being implemented in the code.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • But the delegate is also being set in -viewDidLoad? I didn't include the UIWebView delegate methods I'm using (webViewDidStartLoad, webViewDidFinishLoad, didFailLoadWithError) just to keep the post from being verbose. – Lauren Quantrell May 19 '11 at 12:18
  • Yes you are right. You can safely remove the delegate assignment in `-viewDidLoad`. It, however, has no impact on the retain count of the object as delegate is define as an `assign` property. – Deepak Danduprolu May 19 '11 at 13:08
  • The webview is released twice (autorelease + release) as we are claiming ownership twice. Once while instantiating the object and once when setting the `webView` property. We are relinquishing ownership only once in `-viewDidLoad` and are keeping a reference to the object. This way we make sure the object stays around for as long as we need it. – Deepak Danduprolu May 19 '11 at 13:11
  • However if you can also do this, `myWebView = [[UIWebWebView alloc] initWithFrame:webFrame];` directly. – Deepak Danduprolu May 19 '11 at 13:14
  • When closing this view the myWebView retainCount = 2 after [myWebView release]; so I see I have a problem still. – Lauren Quantrell May 19 '11 at 13:20
  • Relying on the value passed by retainCount isn't the right thing. Just make sure autorelease/release for every alloc/init and retain. – Deepak Danduprolu May 19 '11 at 13:27
  • It is possible that the framework also retains/releases, so retainCount doesn't give us the full picture. – Deepak Danduprolu May 19 '11 at 13:27