使用自定义XIB的自定义UIView上的EXC_BAD_ACCESS [英] EXC_BAD_ACCESS on custom UIView with custom XIB
问题描述
我正在使用最新的SDK开发iOS 5+应用。
我创建了一个自定义UIView( TopMenuView
)使用自定义XIB。在Interface Builder上,我已在此XIB上将 UIView
类更改为 TopMenuView
。我没有设置任何文件的所有者
。
On TopMenuView.m
我有:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
NSLog(@init with coder:%d,counter);
counter ++;
//添加自定义XIB
NSArray * topMenuView = [[NSBundle mainBundle] loadNibNamed:@TopMenuView
所有者:nil
选项:nil];
UIView * nv = [topMenuView objectAtIndex:0];
[self addSubview:nv];
}
返回自我;
}
使用Interface Builder我添加了 UIView
到 UIViewController
并将此 UIView
类更改为 TopMenuView
。
但是,当我运行应用程序时,我收到4251次此日志消息:
2013-10- 13 20:49:34.078 MyProject [470:c07]使用编码器初始化:0
然后,我得到 EXC_BAD_ACCESS
这里:
NSArray * topMenuView = [[NSBundle mainBundle] loadNibNamed:@TopMenuView
所有者:nil
选项:nil];
我就是这样做的:
//将自定义视图添加到我的viewcontroller主视图
self.customNavView = [[CustomNavigationView alloc] init];
self.customNavView = [[[NSBundle mainBundle] loadNibNamed:@CustomNavigationViewowner:self options:nil] objectAtIndex:0];
[self.customNavView setFrame:CGRectMake(0,20,320,54)];
[self.view addSubview:self.customNavView];
这里CustomNavigationView是一个UIView子类,文件所有者类为UIView,UIView自定义类为CustomNavigationView。 / p>
这对我有用。
I'm developing an iOS 5+ app with latest SDK.
I have created a custom UIView (TopMenuView
) with a custom XIB. On Interface Builder I have changed, on this XIB, UIView
class to TopMenuView
. I haven't set any File's Owner
.
On TopMenuView.m
I have:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
NSLog(@"init with coder: %d", counter);
counter++;
// Add custom XIB
NSArray *topMenuView = [[NSBundle mainBundle] loadNibNamed:@"TopMenuView"
owner:nil
options:nil];
UIView *nv = [topMenuView objectAtIndex:0];
[self addSubview:nv];
}
return self;
}
Using Interface Builder I have added a UIView
to a UIViewController
and changed this UIView
class to TopMenuView
.
But, when I run the app, I get this log message 4251 times:
2013-10-13 20:49:34.078 MyProject[470:c07] init with coder: 0
And then, I get an EXC_BAD_ACCESS
here:
NSArray *topMenuView = [[NSBundle mainBundle] loadNibNamed:@"TopMenuView"
owner:nil
options:nil];
This is how I did it:
//Add Custom View to my main view of viewcontroller
self.customNavView = [[CustomNavigationView alloc] init];
self.customNavView = [[[NSBundle mainBundle] loadNibNamed:@"CustomNavigationView" owner:self options:nil] objectAtIndex:0];
[self.customNavView setFrame:CGRectMake(0, 20, 320, 54)];
[self.view addSubview:self.customNavView];
Here CustomNavigationView is a UIView subclass with Files Owner Class as UIView and the UIView custom class as CustomNavigationView.
This works for me.
这篇关于使用自定义XIB的自定义UIView上的EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!