[英]UIView: Loading from custom XIB causing a crash
Hello StackOverflow.你好 StackOverflow。
UIView
such that it loads itself from XIB
file in Xcode.UIView
,以便它从 Xcode 中的XIB
文件加载自己。 To do this, I went through the following initial steps:为此,我经历了以下初始步骤:
UIView
subclass.UIView
子类。XIB
file.XIB
文件。 Then, I added all the subviews I wanted into the XIB
file, and made corresponding IBOutlet Properties
inside the UIView
Subclass.然后,我将我想要的所有子视图添加到
XIB
文件中,并在UIView
子类中制作相应的IBOutlet Properties
。
I watched this video to show me how to properly connect the outlets and set the files owner.我观看此视频是为了向我展示如何正确连接插座并设置文件所有者。 The video instructs me to do the following things:
视频指导我做以下事情:
XIB
.XIB
的子类。 Time linkFile's Owner
to your UIView
subclass in XIB
: Time LinkFile's Owner
设置为XIB
的UIView
子类:时间链接initWithCoder
like (image) if you intend to initialize the custom UIView
within another XIB
file.XIB
文件中初始化自定义UIView
请像 (image) 一样覆盖initWithCoder
。initWithFrame
like (image) if you intend to initialize the custom UIView
programatically within another class file.UIView
则覆盖initWithFrame
like (image) 。 Cool, I have done all of these things, and am choosing to initialize my UIView
programatically.酷,我已经完成了所有这些事情,并且选择以编程方式初始化我的
UIView
。
See my UIView
subclass implementation file :查看我的
UIView
子类实现文件:
#import "CXHostsTableViewCellContentView.h"
@implementation CXHostsTableViewCellContentView
#pragma mark Custom Initializers
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[[NSBundle mainBundle]loadNibNamed:@"CXHostsTableViewCellContentView" owner:self options:nil];
[self setBounds:self.view.bounds];
[self addSubview:self.view];
}
return self;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[[NSBundle mainBundle]loadNibNamed:@"CXHostsTableViewCellContentView" owner:self options:nil];
[self addSubview:self.view];
}
return self;
}
And of course, my header file :当然,我的头文件:
#import <UIKit/UIKit.h>
#import "CXStyleView.h"
@interface CXHostsTableViewCellContentView : UIView
#pragma mark UIView Properties
@property (strong, nonatomic) IBOutlet UIView *view;
@property (nonatomic,weak)IBOutlet UIView *standardView;
@end
I also have an image here of the XIB file's owner and another of the IBOutlet
link from the base UIView
to the outlet on file's owner.我这里还有一张 XIB 文件所有者的图像和另一个从基础
UIView
到文件所有者的插座的IBOutlet
链接。
Right, so everything's looking pretty good, should be no problem running this right?是的,所以一切看起来都不错,运行这个应该没问题吧?
Nope, whenever I initialize this subview and present it, I get a crash:不,每当我初始化这个子视图并呈现它时,我都会崩溃:
CXHostsTableViewCellContentView *scrollContentView = [[CXHostsTableViewCellContentView alloc]init];
I've really got no idea how to solve this, as I'm sure I'm following all of these steps right.我真的不知道如何解决这个问题,因为我确定我正在正确地遵循所有这些步骤。 I've googled and come across this question which has the same symptoms, but the answer has identical code to what I'm using, and this question with a contradictory reply.
我在谷歌上搜索并遇到了这个具有相同症状的问题,但答案与我使用的代码相同,并且这个问题的回答相互矛盾。
I'm not sure what to do at this point, or what is causing the crash.我不知道此时该做什么,或者是什么导致了崩溃。 I know that if I have NO outlets linked at all, it works.
我知道,如果我根本没有连接任何网点,它就可以工作。 But then again, nothing displays either.
但话又说回来,也没有任何显示。
I think that You will face Problem When You Allocate Memory to Your scrollContentView object.我认为当您为 scrollContentView 对象分配内存时会遇到问题。 so,Try To allocate Memory With Frame.
所以,尝试用帧分配内存。
ie Write this in .m file即在 .m 文件中写这个
- (void)myAllocation {
//do your stuff
}
- (id)initWithFrame:(CGRect)aRect {
self = [super initWithFrame:aRect];
if (self) {
[self myAllocation];
}
return self;
}
- (id)initWithCoder:(NSCoder*)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self myAllocation];
}
return self;
}
... ...
CXHostsTableViewCellContentView *scrollContentView = [[CXHostsTableViewCellContentView alloc] initWithFrame:CGRectMake(0, 10, 0, 20)];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.