UIWebView委托代码看起来不对

时间:2011-05-19 12:00:50

标签: xcode uiwebview uiwebviewdelegate

我已经使用我找到的UIWebView实现了示例代码,但它看起来并不适合我,尽管它有效。特别是因为它设置了两次UIWevView委托(在viewDidLoad和viewWillAppear中)。此外,myWebView被设置为自动释放对象,但随后它在dealoc中发布。我希望有人能告诉我如何清理它。

// *** 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];
}

1 个答案:

答案 0 :(得分:0)

这样做真的很好。这个想法很基本。您不希望代表在屏幕外接收任何委派消息。因此,设置委托-viewWillAppear:-viewWillDisappear:的想法是正确的。但我没有看到代码中实现了任何委托方法。