开发者

How to get rid of EXC_BAD_ACCESS

So, another EXC_BAD_ACCESS topic on StackOverflow, but as I'm new to Objective-C this is st开发者_如何学JAVAill a topic I don't really grasp yet. Even though I have done a lot of research about it already.

The issue is the following. I have a UIScrollView that I have overwritten using a Custom Class (named MultiSelectView). If the user taps this UIScrollView and then I want to open a view that allows him to select some data.

So I have declared a UITapGestureRecognizer that calls the openMultiSelect: method. But on the line [parent.navigationController pushViewController:view animated:YES]; I get a Program received signal: "EXC_BAD_ACCESS". error. Why o why?

- (id) initWithCoder:(NSCoder *) coder {
    self = [super initWithCoder: coder];

    if (self) {
        // Add a Tap Gesture Recognizer to the Scrollview.
        // If the user taps the view, it triggers the 'openMultiSelect' method.
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openMultiSelect:)];
        [singleTap setNumberOfTapsRequired:1];
        [singleTap setNumberOfTouchesRequired:1];
        [self addGestureRecognizer:singleTap];
    }

    return self;
}

- (void)openMultiSelect:(UIGestureRecognizer *)gesture {
    //int myViewTag = gesture.view.tag;  // now you know which view called

    DataSelectView *view = [[DataSelectView alloc] initWithNibName:@"DataSelectView" bundle:[NSBundle mainBundle]];
    view.allowMultiSelect = YES;

    [parent.navigationController pushViewController:view animated:YES];
    [view release];
}

So the parent that you see is a ViewController that contains the tab. Is there a better way to do this? Because for now I have thus a ViewController that contains Tabs. In its activateTab: method I thus create the tab and pass self along. I do the same in the viewDidLoad for that tab to pass the parent to the custom UIScrollView:

- (void) activateTab:(int)index {
   ... code ...

   self.tab_Basic = [[TabBasic alloc] initWithNibName:@"TabBasic" bundle: [NSBundle mainBundle]];
   self.tab_Basic.parent = self;

   ... code ...
}


You should make some change to your callback method. Something like that:

- (void)openMultiSelect:(UIGestureRecognizer *)gesture {
    //int myViewTag = gesture.view.tag;  // now you know which view called
    if(gesture.state == UIGestureRecognizerStateEnded){
        DataSelectView *view = [[DataSelectView alloc] initWithNibName:@"DataSelectView" bundle:[NSBundle mainBundle]];
        view.allowMultiSelect = YES;

        [parent.navigationController pushViewController:view animated:YES];
        [view release];
    }
}


What you are doing wrong is releasing the object "view" too early, don't release it until the view is popped. That should fix the problem.

- (void)openMultiSelect:(UIGestureRecognizer *)gesture {
//int myViewTag = gesture.view.tag;  // now you know which view called

DataSelectView *view = [[DataSelectView alloc] initWithNibName:@"DataSelectView" bundle:[NSBundle mainBundle]];
view.allowMultiSelect = YES;

[parent.navigationController pushViewController:view animated:YES];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜