objective-cxcodeuiimageviewinitwithcoder

IBOutlet is nil after initWithCoder is called


Simple problem, I have defined a UIImageView, called bigImageView in a UIViewController using the storyboard, It's declared in the h file of that UIViewController as follows:

 @property (retain, nonatomic) IBOutlet UIImageView *bigImageView;

on my appDelegate I init the UIViewController as follows:

imageViewController = [storyboard instantiateViewControllerWithIdentifier:@"chosenImageController"];

this calls initWithCoder on my UIViewController m file:

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Custom initialization
    }
    return self;
}

This function is only called once so there's no double init. However, later, when I check my bigImageView pointer, it's still nil.

Isn't the init supposed to allocate memory to it? I think that's why when I try to set this UIImageview to hold a UIImage it doesn't display the image Thanks


Solution

  • You don't need to define initWithCoder, since you have no custom logic in there. I would delete that boilerplate code.

    Here is what I would check:

    1. In the storyboard, ensure that the class of the view controller is set properly.
    2. Ensure that the outlet is hooked up properly in the storyboard by looking for a circle near your @property. It should be a filled in circle, not an outline of a circle.
    3. Make sure you are reading the value only after viewDidLoad is called. Apple's only guarantee is that the outlet is set after this method call.

    Update: It sounds like you want to access the image view before the view is loaded. There is no way to do this. One hack is to call viewController.view which will force the view to load, but there are many reasons why you should not do this.

    A better approach would be to implement properties on your view controller which work for both when the view is not loaded and when the view is loaded. You can see an example of an elegant solution in this question. Notice how if the view is loaded, the photographerLabel will get set via the didSet method. On the other hand, if the view is not loaded, it will get set via the viewDidLoad method. For an Objective-C version of that code or for more details, see the linked video in that question.