简体   繁体   English

在initWithCoder中获取EXEC_BAD_ACCESS

[英]get EXEC_BAD_ACCESS inside initWithCoder

I have a problem during loading/creating a new view form xib file. 在加载/创建新的视图表单xib文件时遇到问题。 I get EXEC_BAD_ACCESS code=2 and on my call stack i see multiple call of initWithCoder method. 我得到EXEC_BAD_ACCESS code = 2并且在我的调用堆栈上,我看到initWithCoder方法的多次调用。 I do not understand why it is happened since i call alloc init for my class only once. 我不明白为什么会这样,因为我只为我的类调用alloc init。 Below my code: 在我的代码下面:

@interface SongListView :  NSView
@property (nonatomic, strong) IBOutlet NSScrollView* view;
@end

@implementation SongListView

- (id)initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];

    if(self)
    {
        [self setup];
    }
    return self;
}

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

    if(self)
    {
        [self setup];
    }

    return self;
}

- (void)setup
{
    NSArray *nib;

    [[NSBundle mainBundle] loadNibNamed:@"scrollTabViewItemWithTableView"
                                  owner:self
                        topLevelObjects:&nib];
    [self addSubview: self.view];
}

@end

and this is how I create SongListView: 这就是我创建SongListView的方法:

SongListView* tableView = [[SongListView alloc] init];

Can someone explain me what happens, why i get mutiple call of initWithCoder method, and how to fix it? 有人可以解释我发生了什么,为什么我多次调用initWithCoder方法,以及如何解决它?

Generally your NSView subclasses shouldn't load XIBs. 通常,您的NSView子类不应加载XIB。 They should either be IN a XIB (and thus created when the XIB loads) or created in code. 它们应该在XIB中(并在XIB加载时创建)或在代码中创建。

In your case there are a number of architecturally strange things, but I suspect the crash is caused by you having a SongListView instance inside scrollTabViewItemWithTableView.xib, so when you load the XIB you run setup and load the XIB and run setup and load the XIB and run setup and... 在您的情况下,有许多体系结构上的奇怪问题,但是我怀疑崩溃是由您在scrollTabViewItemWithTableView.xib中具有SongListView实例引起的,因此,当您加载XIB时,请运行安装程序并加载XIB并运行安装程序并加载XIB然后运行安装程序并...

You are trying to add your view as a subview of itself: 您正在尝试将视图添加为自身的子视图:

[self addSubview:self.view];

Code like this hurts my eyes. 这样的代码会伤害我的眼睛。 You have a SongListView object, but you've named it tableView . 您有一个SongListView对象,但已将其命名为tableView You've defined an initialiser for initWithFrame: but you are sending it the init message when you are creating it. 您已经为initWithFrame:定义了一个初始化程序initWithFrame:但是您在创建它时将其发送给init消息。

Loading nib causes a call to -initWithCoder: on the related class and passing self as owner assigns the top-level view in nib as self. 加载nib会导致在相关类上调用-initWithCoder:并将self传递为所有者将nib中的顶级视图分配为self。 You have a recursion because with each load, you try to load the view again. 您有一个递归,因为每次加载都会尝试再次加载视图。

Once you loaded the nib, in -initWithCoder: your view is already loaded, and you don't have to load nib or add subview. 加载笔尖后,在-initWithCoder:您的视图已加载,无需加载笔尖或添加子视图。

What usually goes in setup are some UI additions, or some others things that couldn't be done in IB. setup中通常会添加一些UI,或者在IB中无法完成的其他一些事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM