Я реализую простой браузер в приложении.В моем домашнем представлении (UITableViewController
) у меня есть что-то вроде:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WebViewController *webViewController = [[WebViewController alloc] init];
switch (indexPath.row) {
case 0:
webViewController.stringURL = @"http://www.google.com";
break;
case 1:
webViewController.stringURL = @"http://www.bing.com";
break;
default:
webViewController.stringURL = @"http://stackoverflow.com";
break;
}
[self.navigationController pushViewController:webViewController animated:YES];
[webViewController release];
}
Приложение зависло после того, как я несколько раз перемещался назад и вперед между моим домашним видом и webViewController
несколькимираз.
В классе WebViewController
у меня нет ничего, кроме [UIWebView *webView]
и [UIActivityIndicatorView *activityIndicator]
.Оба с атрибутами nonatomic, retain
.Вот реализация.
#import "WebViewController.h"
@implementation WebViewController
@synthesize webView, activityIndicator, stringURL;
- (void)dealloc
{
[self.webView release];
self.webView.delegate = nil;
[self.activityIndicator release];
[super dealloc];
}
-(void)loadView {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = contentView;
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webFrame.origin.y = 0.0f;
self.webView = [[UIWebView alloc] initWithFrame:webFrame];
self.webView.backgroundColor = [UIColor blueColor];
self.webView.scalesPageToFit = YES;
self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.webView.delegate = self;
[self.view addSubview: self.webView];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.stringURL]]];
self.activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.activityIndicator.frame = CGRectMake(0.0, 0.0, 30.0, 30.0);
self.activityIndicator.center = self.view.center;
[self.view addSubview: self.activityIndicator];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadView];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
// starting the load, show the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
// finished loading, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[activityIndicator stopAnimating];
}
@end
Я только что запустил свое приложение в Инструментах, используя шаблон Zombies , который показывает, что -[UIWebView webView:didReceiveTitle:forFrame:]
- это вызов Zombie.Но я все еще не могу понять, в чем на самом деле проблема.
(Пожалуйста, скачайте след при необходимости)
Любая помощь очень ценится!
[Update]:
- Как отметили @ 7KV7 и @David, в моей функции
dealloc
есть очевидная ошибка.Я должен позвонить self.webView.delegate=nil;
сначала, прежде чем отпустить self.webView
.Извини за это.К сожалению, после того, как я это исправлю, приложение все равно вылетает так же. - Если я удаляю
[webViewController release];
из первого блока кода, сбой фактически исчезает.Но очевидно, что будет утечка памяти.