2011-07-09 44 views
1

试图拦截父UIView,这是封装UIScrollView,和另一个UIView(覆盖),并排坐在(意味着顶部的触摸事件彼此在相同的容器视图中)。截至目前,我只是想打印出父的UIView中的touchesBegan事件的确认的结果,但它抛出EXC_BAD_ACCESS,与此错误:EXC_BAD_ACCESS:当试图处理触摸事件父母UIVIew

Program received signal: “EXC_BAD_ACCESS”. 
    warning: Unable to restore previously selected frame. 
    Data Formatters temporarily unavailable, will re-try after a 'continue'. (Not safe  
    to call dlopen at this time.) 

我认为所有的相关的代码应该是下面。如果更容易看到,我也将整个项目上传到http://devmu.com/transfer/NoteMap.zip

的loadView中的UIViewController:

- (void)loadView { 
    [super loadView]; 

    [[NSBundle mainBundle] loadNibNamed:@"ContainerView" owner:self options:nil];  
    ContainerView *container = self.containerView = [[ContainerView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; 

    self.view = container; 
    //[self.view addSubview:container]; 

    [container release]; 
}; 
-the dealloc function releases the containerView 

ContainerView:

@implementation ContainerView 

@synthesize overlayView=_overlayerView, scrollView=_scrollView; 

- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     //setup scrollview 
     [[NSBundle mainBundle] loadNibNamed:@"GridScrollView" owner:self options:nil]; 
     self.scrollView = [[GridScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
     [self addSubview:self.scrollView]; 

     //setup overlayview 
     NSArray *nibOverlayContents = [[NSBundle mainBundle] loadNibNamed:@"MapOverlayView" owner:self options:nil]; 
     self.overlayView = [nibOverlayContents objectAtIndex:0]; 
     [self addSubview:self.overlayView];  
    } 
    return self; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesBegan:touches withEvent:event]; 
    NSLog(@"ContainerView touchesBegan"); 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSLog(@"ContainerView touchesEnded"); 
} 

- (void)dealloc { 
    [self.overlayView release]; 
    [self.scrollView release]; 
    [super dealloc]; 
} 

我有单独的榫文件的ContainerView(从控制器加载),和它的两个子视图,MapView类和MapOverlayView(从ContainerView的init加载)。不知道它是否应该重要......但是nib文件只能引用其包含的视图。即。 ContainerView.xib的文件所有者是UIViewController,只有视图才是ContainerView。 MapView.xib和MapOverlayView.xib的文件所有者是ContainerView,它包含两个对MapView和MapOverlayView(ContainerView/File Owner的IBOutlets)的引用。

这可能是什么原因造成的?

感谢您的任何帮助。

+1

你不应该使用在dealloc的方法访问方法。 [self.overlayView版本]。使用[overlayView发布]。 – LuckyLuke

+0

一些loadNibNamed:调用根本没有意义。另外,你正在泄漏GridScrollView。 – Eiko

回答

-1

注释语句

[container release]; 

和释放使用的NSObject的的dealloc()由容器对象分配的资源,从而

-(void) dealloc 
{ 
    [self.containerView dealloc]; 
} 
+1

切勿直接调用另一个对象的'dealloc'方法。另外,你必须在实现结束时调用'[super dealloc]'(除非在ARC下)。 – albertamg