I have a xib file called "BMSkipBreakButton.xib" that just contains a UILabel and a UIImageView.
I set the File Owner of this xib to a class called BMSkipBreakButtonView:
When I go to the Connections Inspector for the File Owner it shows up like this:
Each of UI elements in the XIB have a referencing outlet that connects to the File Owner. For example:
The class BMSkipBreakButtonView looks like this (the outlets are hooked up to the UI elements inside the XIB):
.h:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface BMSkipBreakButtonView : UIView
@property (nonatomic, strong) IBOutlet UILabel *skipBreakButtonText;
@property (nonatomic, strong) IBOutlet UIImageView *skipImageView;
@end
NS_ASSUME_NONNULL_END
The .m file looks like this:
#import "BMSkipBreakButtonView.h"
@interface BMSkipBreakButtonView ()
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) NSMutableArray *customConstraints;
@end
@implementation BMSkipBreakButtonView
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
_customConstraints = [[NSMutableArray alloc] init];
UIView *view = nil;
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"BMSkipBreakButton"
owner:self
options:nil];
for (id object in objects) {
if ([object isKindOfClass:[UIView class]]) {
view = object;
break;
}
}
if (view != nil) {
_containerView = view;
view.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:view];
[self setNeedsUpdateConstraints];
}
}
- (void)updateConstraints
{
[self removeConstraints:self.customConstraints];
[self.customConstraints removeAllObjects];
if (self.containerView != nil) {
UIView *view = self.containerView;
NSDictionary *views = NSDictionaryOfVariableBindings(view);
[self.customConstraints addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat:
@"H:|[view]|" options:0 metrics:nil views:views]];
[self.customConstraints addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat:
@"V:|[view]|" options:0 metrics:nil views:views]];
[self addConstraints:self.customConstraints];
}
[super updateConstraints];
}
@end
The problem is that the app is crashing at the "loadNibNamed" method call (I set an exception breakpoint and the app stops at this breakpoint). I cannot for the life of me figure out why it's crashing. I have checked the connections for the File Owner and the outlets and cannot see anything wrong. I've also checked the name of the XIB to make sure I didn't spell it wrong in the loadNibNamed method.
The only thing indicated in the debugger console is:
"libc++abi.dylib: terminating with uncaught exception of type NSException"
I turned OS_ACTIVITY_MODE off and then received more info in the debugger console:
Uncaught exception: Could not load NIB in bundle: 'NSBundle </var/containers/Bundle/Application/C377C361-A15B-467A-A6AA-F3DFBB5CC285/TestApp.app> (loaded)' with name 'BMSkipBreakButton'
And I also see...
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </var/containers/Bundle/Application/C377C361-A15B-467A-A6AA-F3DFBB5CC285/TestApp.app> (loaded)' with name 'BMSkipBreakButton''
*** First throw call stack:
(0x1ea3ca180 0x1e95a29f8 0x1ea2d43a0 0x2162edb60 0x2162ee9dc 0x1008f0fa8 0x1008f0e78 0x100919198 0x1009196a4 0x21608a224 0x21608a628 0x10085c244 0x10085b498 0x21608a224 0x21608a628 0x1008fe0dc 0x100891ccc 0x100890e10 0x1008aa434 0x1002d6f10 0x100289ad4 0x10256b6f4 0x10256cc78 0x10257a6fc 0x1ea35bb20 0x1ea356a58 0x1ea355fb4 0x1ec55779c 0x216633c38 0x1002bc190 0x1e9e198e0)
libc++abi.dylib: terminating with uncaught exception of type NSException
Can someone help?
Update:
I noticed that the XIB did not have a target membership. I added it, but it still crashes: