UIWebView 委托代码似乎不正确

发布于 2024-11-07 22:26:02 字数 2074 浏览 5 评论 0原文

我已经使用我找到的 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];
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉酒的小男人 2024-11-14 22:26:02

这样做非常好。这个想法非常基本。您不希望代理人在屏幕外时收到任何委托消息。所以设置委托-viewWillAppear:-viewWillDisappear:的想法是正确的。但我没有看到代码中实现任何委托方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文