一个关于iOS下程序启动后自动退出的问题。

今天在学习iOS开发中碰到一个问题,如下:

有如下代码,编译不会报错,但是一进入程序后就会自动退出。

当我注释掉 - (void)loadView 中的这句:web.delegate = self;的时候,程序就不会自动退出了。

我试过清空- (void) webViewDidStartLoad:(UIWebView *) webView  和 - (void)webViewDidFinishLoad:(UIWebView *) webView 中的代码,

当这2个方法为空的时候,程序启动后同样会自动退出

只有将web.delegate = self;注释掉,程序才不会自动退出。

这个问题困扰了我很长时间,也去过CocoaChina求助,但是虽然围观的人很多,却没人帮我解决问题。

最后我又梳理了一遍代码,终于找到问题所在。

原来是设置为delegate的那个class被我过早的release掉了..

代码部分为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
- (void) webViewDidStartLoad:(UIWebView *) webView {
	if (myAlert==nil){
		myAlert = [[UIAlertView alloc] initWithTitle:nil
								message: @"Loadding..."
								delegate: self
								 cancelButtonTitle: nil
								 otherButtonTitles: nil];
 
		UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
		activityView.frame = CGRectMake(120.f, 48.0f, 37.0f, 37.0f);
		activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
		[myAlert addSubview:activityView];
		[activityView startAnimating];
		[myAlert show];
	}
 
	NSLog(@"start load web");  
}
 
 
- (void)webViewDidFinishLoad:(UIWebView *) webView {
	[myAlert dismissWithClickedButtonIndex:0 animated:YES];
	[myAlert release];
	myAlert=nil;
	NSLog(@"finish load web");
}
 
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
 
}
 
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
	return YES;
}
 
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
 
	web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
	web.scalesPageToFit = YES;
	web.delegate = self;
 
	[self.view addSubview:web];	
}
 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[web loadRequest:[NSURLRequest requestWithURL:[[[NSURL alloc] initWithString:@"http://www.google.com"] autorelease]]];
 
}

“一个关于iOS下程序启动后自动退出的问题。”   2 comments

期待你的作品……
avatar
什么时候有自己的作品 我去帮你推荐 呵呵
avatar

Leave a comment:

XHTML: You can use these tags: <a href="" title=""> <b> <blockquote cite=""> <em> <i> <strong> <p> <br><br />