如何摆脱 EXC_BAD_ACCESS
所以,这是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该对回调方法进行一些更改。像这样的东西:
You should make some change to your callback method. Something like that:
你做错的是太早释放对象“视图”,在弹出视图之前不要释放它。这应该可以解决问题。
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.