推送视图控制器上的自定义后退按钮单击事件

时间:2010-04-03 14:33:33

标签: iphone uinavigationcontroller uibutton

我已经推动了视图控制器,并使用编程方式将右下角的WebView和自定义矩形圆形按钮加载到视图中。

-(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消息。

如何解决这个问题?

谢谢,

1 个答案:

答案 0 :(得分:0)

您在这里-autorelease'd 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的每次调用都不是免费的。