IBOutlet is nil inside custom UIView (Using STORYBOARD)
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.
Comments
-
padam thapa over 3 years
I have a custom
UIView
class. Inside it I have declared anIBOutlet
property forUIImageView
.#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 oneUIImageView
as a subview of aboveUIView
. I set the "SettingItem" class toUIView
from storyboard. I connected the outlet to myImage by normal dragging from outlets ofSettingItem
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 alwaysnil
, aboveNSLog
just print(null)
for the outlet. I checked the view in storyboard and checked its outlet reference and its pointing tomyImage
. 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 over 11 yearsthen how am i supposed to connect outlet to it from storyboard since property is not visible anymore ?
-
OnkaPlonka over 11 yearsif you click on the status bar of the view and then go to the connections inspector then link it
-
padam thapa over 11 yearsThanks but still getting null
-
padam thapa over 11 yearsviewWillAppear doesn't belong to UIView, is it ? Its in UIViewController.
-
Fabio Poloni over 11 years@OnkaPlonka You're wrong. The
IBOutlet
in the@property
-declaration does mean, it's available in theUIStoryBoard
. You shouldn't use@interface Class : SuperClass { IBOutlet ... }
anymore. -
Fabio Poloni over 11 years@OnkaPlonka Of course it's not wrong but it's not the solution.
-
cbowns over 10 yearsThose 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 namedmyImage
), but second and more importantly, this has nothing to do with the question being asked. -
lagos over 9 yearsBut you are not answering his question.
-
MANIAK_dobrii over 9 yearsShort: 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 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 about 8 yearsIn 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