IBOutlet is nil inside custom UIView (Using STORYBOARD)

20,005

Solution 1

Override awakeFromNib in your view - this is the first lifecycle method to be called after the IBOutlets have been assigned and is the right place for your code.

Solution 2

initialize them in awakeFromNib:

- (void)awakeFromNib
{
    //init code
}

Solution 3

I just had the exact same problem and the solution is really easy:

You're accessing myImage to soon - that's it.

Withing -(id) initWithCoder:(NSCoder *)aDecoder{ and - (id)initWithFrame:(CGRect)frame the UIView is not already drawed. So myImage is not initalized yet.

You can test it if you add a UIButton and an IBAction and do something like this:

- (IBAction)buttonClicked:(id)sender {
    NSLog(@"%@",myImage);
}

And you'll see how myImage is not nil anymore.

Share:
20,005
padam thapa
Author by

padam thapa

https://padamthapa.com

Updated on November 29, 2020

Comments

  • padam thapa
    padam thapa over 3 years

    I have a custom UIView class. Inside it I have declared an IBOutlet property for UIImageView.

    #import <UIKit/UIKit.h>
    
    @interface SettingItem : UIView{
    
    }
    
    @property (strong, nonatomic) IBOutlet UIImageView *myImage;
    
    @end
    

    Now i am using storyboard. There is a viewcontroller. I dragged UIView to viewcontroller. I dragged one UIImageView as a subview of above UIView. I set the "SettingItem" class to UIView from storyboard. I connected the outlet to myImage by normal dragging from outlets of SettingItem from utilities window.


    SettingItem implementation

    #import "SettingItem.h"
    
    @implementation SettingItem
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
            [self baseInit];
        }
        return self;
    }
    
    -(id) initWithCoder:(NSCoder *)aDecoder{
        self = [super initWithCoder:aDecoder];
        if (self) {
            // Initialization code
            [self baseInit];
        }
        return self;
    }
    
    - (void) baseInit{
        NSLog(@"myImage %@"self.myImage);
    }
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
    }
    */
    
    @end
    

    Now my problem is myImage is always nil, above NSLog just print (null) for the outlet. I checked the view in storyboard and checked its outlet reference and its pointing to myImage. I am missing something. I googled for the help but couldn't find any solution.

    Can you please point out what am i doing wrong ?

  • padam thapa
    padam thapa over 11 years
    then how am i supposed to connect outlet to it from storyboard since property is not visible anymore ?
  • OnkaPlonka
    OnkaPlonka over 11 years
    if you click on the status bar of the view and then go to the connections inspector then link it
  • padam thapa
    padam thapa over 11 years
    Thanks but still getting null
  • padam thapa
    padam thapa over 11 years
    viewWillAppear doesn't belong to UIView, is it ? Its in UIViewController.
  • Fabio Poloni
    Fabio Poloni over 11 years
    @OnkaPlonka You're wrong. The IBOutlet in the @property-declaration does mean, it's available in the UIStoryBoard. You shouldn't use @interface Class : SuperClass { IBOutlet ... } anymore.
  • Fabio Poloni
    Fabio Poloni over 11 years
    @OnkaPlonka Of course it's not wrong but it's not the solution.
  • cbowns
    cbowns over 10 years
    Those two declarations are, firstly, equivalent with respect to their ivar storage (modulo the ivar's name: in the first one, the property will be auto-synthesized to _myImage, whereas the second one is named myImage), but second and more importantly, this has nothing to do with the question being asked.
  • lagos
    lagos over 9 years
    But you are not answering his question.
  • MANIAK_dobrii
    MANIAK_dobrii over 9 years
    Short: this is the only correct answer here, others should be wrapped into lead cover and sent to space. Long: Oh, how I hope when I'm going to ask anything on SO, there would be somebody like @Oly to provide the only reasonable and correct answer. Other answers are so rubbish, that it just demonstrates absence of required knowledge. Please, be more like Oly and less like the rest in this thread.
  • Oly Dungey
    Oly Dungey about 9 years
    @MANIAK_dobrii You've just cheered up my day, I've just returned to work after a horrible accident last year and needed some encouragement ;-)
  • Mehul Thakkar
    Mehul Thakkar about 8 years
    In awakefromNib, it is still getting null values, i have searched many things on stackoverflow, and answer i got at many links is that content is lazy loaded, so in awakefromnib, you can have iboutlets values null