IBOutlets are nil on a XIB view - objective-c

I have a UIView subclass and it's corresponding XIB. I use this UIView in a UITableViewCell designed in Interface Builder. When dequeuing the cells, I have a correct reference from the cell to the custom view but the IBOutlets of the custom view are nil.
Here is a sample project that shows the problem
https://github.com/JanC/TestXIB
What am I doing wrong?
cheers,
Jan

I don't think it exactly works that way with UITableViewCell, defining it as IBOutlet won't load all the subviews so you have to load it from the bundle using [NSBundle mainBundle] loadNibNamed:#"" owner:self]; and assigned that to your item view

Now it's loading correctly. In the xib, I set the file owner to TAItemTitleView and the Class to just a UIView. I then load the xib in the TAItemTitleView itself
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
NSString *className = NSStringFromClass([self class]);
self.view = [[[NSBundle mainBundle] loadNibNamed:className owner:self options:nil] firstObject];
[self addSubview:self.view];
}
return self;
}
https://github.com/JanC/TestXIB/blob/master/TestXIB/Classes/Views/TAItemTitleView.m

Related

custom UIVIew with nib subviews nil

I create a simple custom UIView with a xib file, I connect xib view to my class, then I linked my IBOutles to subviews, after this I create the view with initWithFrame and I add it programmatically to my parent view
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"AddAlarmView" owner:self options:nil];
_scrollView = (UIScrollView *)[objects objectAtIndex:0];
[self addSubview:_scrollView];
}
return self;
}
the problem is that all the subviews are nil....if I try to get customView.textFeild1 it is nil....why?where is the mistake?
Instead of this you should try to get your UIView object from Xib like this..
- (id)initWithFrame:(CGRect)frame
{
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"AddAlarmView" owner:self options:nil];
self = (YourClassName *)[objects objectAtIndex:0];
self.frame = frame;
return self;
}
You don't need to add subViews again your IBOutLets. They are already subView of your View in xib file.
Simply load that xib file and assign it to your self and update your frame according to the parameter.

loadNibNamed does not load the subviews of a XIB file

I am using loadNibNamed to init a custom view. But I see that the subviews are not available when the view is returned.
-(instancetype) initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]){
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"HRAlternateCategoriesView" owner:self options:nil];
self = [views firstObject];
[self setFrame:frame];
}
return self;
}
viewDidLoad method
-(void) viewDidLoad {
CGRect frame = CGRectMake(0, 0, 320, 220);
self.altCategoryView = [[HRAlternateCategoriesView alloc] initWithFrame:frame];
[self.view addSubview:self.altCategoryView];
}
The XIB file contains 8 subviews (4 buttons and 4 image views) none of which are there in the generated UIView class. Iam not usng autolayout and size classes.
OS: iOS8

add subview is not displaying my uiview

I have the following code, and the uiview is not showing, i've placed a breakpoint & see the view is not nil and the frame is set to the given size - why the view is not showing in my uiviewcontroller?
#property (nonatomic,strong) LoadingView *loadingView;
self.loadingView = [[[NSBundle mainBundle] loadNibNamed:#"LoadingView" owner:self options:nil] objectAtIndex:0];
self.loadingView.frame = CGRectMake(110,170,100,100);
[self.view addSubview:self.loadingView];
Your code is logically wrong. Your LoadingView class must allocate before you assigning nib member to it.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:#"LoadingView" owner:self options:nil];
self = [nibs objectAtIndex:0];
}
return self;
}
Your nib assignment should be in the initwithframe method of LoadingView class.
Then Your implementation of adding view would be like,
self.loadingView = [[LoadingView alloc] initWithFrame:CGRectMake(110,170,100,100)];
[self.view addSubview:self.loadingView];

iOS: Returning to main XIB from secondary XIB

I am trying to change a back to the main xib after I was on a second XIB. This is the current code I am using:
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:#"Results1" owner:self options:nil];
UIView *aView = [nibObjs objectAtIndex:0];
self.view = aView;
I have a button on the second xib that returns back to the originating xib. When I hit the button the app crashes. I am not sure why because it does not display an error - it just crashes. Here is the code on the button on the second xib or "Results1" xib file:
-(IBAction)Button100:(id)sender
{
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:#"main" owner:self options:nil];
UIView *aView = [nibObjs objectAtIndex:0];
self.view = aView;
}
Not sure what I am doing wrong.
Add as subviews instead, so you can then just 'pop' the Results1 view off:
// in your .h
#property (strong) UIView *aView;
// replace your provided code with this
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:#"Results1" owner:self options:nil];
self.aView = [nibObjs objectAtIndex:0];
[self.view addSubview:self.aView];
- (IBAction)Button100:(id)sender
{
[self.aView removeFromSuperview];
self.aView = nil;
}
Just a suggestion — if you want them to 'swap', consider using the class method transitionFromView:toView:duration:options:completion: in UIView.
The problem is I needed to turn off Automatic Reference Counting.

iOS: assigning a class to a xib, and not recognizing it

I'm using the following code to load a xib in my UIView subclass named ImageWithCaptionView.
I've specified in the xib Inspector the name of the view: ImageWithCaptionView.
However the if statement (isKindOfClass) is never true, and I've to remove it to make things work. What am I missing here ?
UINib *nib = [[UINib nibWithNibName:NSStringFromClass([ImageWithCaptionView class]) bundle:nil] retain];
NSArray *myArray = [nib instantiateWithOwner:self options:nil];
for (id currentObject in myArray) {
if ([currentObject isKindOfClass:[ImageWithCaptionView class]]) {
self = (ImageWithCaptionView *) currentObject;
break;
}
}
Check if the class is also set in the Custom Class field in the identity inspector tab.