0

I normally use xib for custom cells in TableViews with Swift. In the dequeuereusablecellwithidentifier, I can easily access the cell's outlets.

I am working on a project where I created a XIB for a view with a spinner in it (the spinner is custom and is a view). I load said view in my view controller and it works fine:

XIB

#import "HomeScreenView.h"

@interface HomeScreenView()
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImage;
@property (weak, nonatomic) IBOutlet UIImageView *spinner;

@end

@implementation HomeScreenView

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
}

@end

ViewController.m

HomeScreenView *homeScreen = [[[NSBundle mainBundle] loadNibNamed:@"HomeScreenView" owner:self options:nil] firstObject];
homeScreen.frame = self.view.bounds;
_loadingView = homeScreen;
[self.view addSubview:_loadingView];

In the ViewController.m, I cannot access the outlet's spinner: for instance homeScreen.spinner does not autocomplete.

In Swift, with custom cells, I know we load the file and the instantiate the cell and cast them. Since I don't know ObjC properly, I am a bit confused on how to process on how to access the xib's outlets.

standousset
  • 1,092
  • 1
  • 10
  • 25
  • 1
    Because your declaration in the `.m`, Put the `@property (weak, nonatomic) IBOutlet UIImageView *spinner;` in the .h. – Larme Dec 20 '17 at 10:11
  • @Larme, thank you it works fine. Should I put all the outlets in the `.h` of the xib file ? – standousset Dec 20 '17 at 10:17
  • 1
    As said in the linked question, if you need to access the properties outside the class, yes you can do it (or use getter only so that you can't modify it), it really depends on your needs. For most of the cases, you can let them in `.h`, assuming that developers won't do "stupid" things with them. – Larme Dec 20 '17 at 10:25

0 Answers0