0

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

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

所以我声明了一个UITapGestureRecognizer调用该方法的openMultiSelect:方法。但是在线上[parent.navigationController pushViewController:view animated:YES];我得到一个Program received signal: "EXC_BAD_ACCESS".错误。为什么o为什么?

- (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 ...
}
4

2 回答 2

0

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

- (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];
    }
}
于 2011-07-19T15:53:57.260 回答
-1

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

- (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];
于 2011-07-19T15:46:37.407 回答