如何摆脱 EXC_BAD_ACCESS

发布于 2024-11-25 11:02:16 字数 1939 浏览 2 评论 0原文

所以,这是 StackOverflow 上的另一个 EXC_BAD_ACCESS 主题,但由于我是 Objective-C 的新手,这仍然是一个我还没有真正掌握的主题。尽管我已经对此做了很多研究。

问题如下。我有一个 UIScrollView,我已使用自定义类(名为 MultiSelectView)覆盖它。如果用户点击这个 UIScrollView 然后我想打开一个允许他选择一些数据的视图。

因此,我声明了一个调用 openMultiSelect: 方法的 UITapGestureRecognizer 。但在 [parent.navigationController PushViewController:viewanimate:YES]; 线上,我收到一个 Program returned signal: "EXC_BAD_ACCESS". 错误。为什么哦为什么?

- (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];
}

因此,您看到的 parent 是一个包含选项卡的 ViewController。有更好的方法吗?因为现在我有一个包含选项卡的 ViewController。因此,在其 activateTab: 方法中,我创建了选项卡并传递 self 。我在该选项卡的 viewDidLoad 中执行相同操作,将 parent 传递给自定义 UIScrollView

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

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

   ... code ...
}

So, another EXC_BAD_ACCESS topic on StackOverflow, but as I'm new to Objective-C this is still 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 ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧街凉风 2024-12-02 11:02:16

您应该对回调方法进行一些更改。像这样的东西:

- (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];
    }
}

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];
    }
}
冬天旳寂寞 2024-12-02 11:02:16

你做错的是太早释放对象“视图”,在弹出视图之前不要释放它。这应该可以解决问题。

- (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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文