Hello StackOverflow.
- I am trying to setup a
UIViewsuch that it loads itself fromXIBfile in Xcode.
To do this, I went through the following initial steps:
- Created empty
UIViewsubclass. - Added a blank
XIBfile.
Then, I added all the subviews I wanted into the XIB file, and made corresponding IBOutlet Properties inside the UIView Subclass.
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:
- Do not set the UIView class to your subclass in
XIB. Time link - Set the
File's Ownerto yourUIViewsubclass inXIB:Time Link - Insert a IBOutlet into your UIView class of type UIView so your XIB file can load into that.Time link
- Override
initWithCoderlike (image) if you intend to initialize the customUIViewwithin anotherXIBfile. - Override
initWithFramelike (image) if you intend to initialize the customUIViewprogramatically within another class file.
Cool, I have done all of these things, and am choosing to initialize my UIView programatically.
See my UIView subclass implementation file:
#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.
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.
