2010-04-03 25 views
0

我已經推動視圖控制器並使用編程方式將右下角的WebView和Custom矩形圓角按鈕加載到視圖中。在推送的視圖控制器上自定義後退按鈕點擊事件

-(void)loadView { 


     CGRect frame = CGRectMake(0.0, 0.0, 480, 320); 
    WebView = [[[UIWebView alloc] initWithFrame:frame] autorelease]; 
    WebView.backgroundColor = [UIColor whiteColor]; 
    WebView.scalesPageToFit = YES; 
    WebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin); 
    WebView.autoresizesSubviews = YES; 
    WebView.exclusiveTouch = YES; 

    WebView.clearsContextBeforeDrawing = YES; 

self.roundedButtonType = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 
self.roundedButtonType.frame = CGRectMake(416.0, 270.0, 44, 19); 
[self.roundedButtonType setTitle:@"Back" forState:UIControlStateNormal]; 
self.roundedButtonType.backgroundColor = [UIColor grayColor]; 
[self.roundedButtonType addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside]; 

self.view = WebView; 

[self.view addSubview: self.roundedButtonType ]; 
[WebView release]; 


} 

這是我添加爲導航的後退按鈕的操作。

-(void)back:(id)sender{ 
[self.navigationController popViewControllerAnimated:YES]; 
} 

-(void)viewDidUnload{ 
self.WebView = nil; 
self.roundedButtonType = nil; 
} 

-(void)dealloc{ 
[roundedButtonType release]; 
[super dealloc]; 
} 

這裏,當後退按鈕點擊則顯示以前的觀點,但應用陷入了這一觀點,並表示GDB程序接收到的信號:EXC_BAD_ACCESS消息。

如何解決這個問題?

感謝,

回答

0

你有-autorelease倒是WebView這裏:

WebView = [[[UIWebView alloc] initWithFrame:frame] autorelease]; 

但隨後再次-release吧!

[self.view addSubview: self.roundedButtonType ]; 
[WebView release]; 

嘗試刪除其中一個版本。


此外,

self.roundedButtonType = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 

除非roundedButtonType被聲明爲@property(assign,...),你並不需要發送-retain消息。在分配到self.roundedButtonType之前,最好設置框架,標題等,因爲每次撥打self.roundedButtonType都不是免費的。

+0

謝謝,肯尼 我已經刪除了一個autorelease到WebView和它的工作在我的情況很好。 [WebView發佈]是否也會釋放http請求或僅從WebView卸載數據,並且URL連接處於活動狀態? 在分配之前,我還會照顧設置框架和標題。 Thasnk – TechFusion 2010-04-05 05:13:03

+0

@Tech:當Web視圖爲「-dealloc」時,所有Web視圖擁有的所有內容都將「釋放」(note release≠deallocate)。在你的情況下,'self'會在分配給'self.view'時保留Web視圖,所以''釋放''它不會中斷連接。 – kennytm 2010-04-05 06:20:21

+0

你好Kenny, 我也在viewDidUnload()中使self.WebView = nil。所以要殺死連接,我需要在dealloc任務中添加以下內容: - (void)dealloc {super dealloc] [self.WebView dealloc] //這是否正確? } 謝謝, – TechFusion 2010-04-06 03:57:54

相關問題