IBOutlet is nil inside custom UIView (Using STORYBOARD)

天大地大妈咪最大 提交于 2019-12-29 20:27:41

问题


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 ?


回答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.




回答2:


initialize them in awakeFromNib:

- (void)awakeFromNib
{
    //init code
}



回答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.




回答4:


I had similar problems, but I finally figured out what was wrong - It was a difference in the idiom between XIB files and Storyboards.

In all the tutorials that I had seen with Xib files, if I had a UITableViewController create a DetailViewController to let me edit the content of the items in the table, then the tableViewController created an instance of the DVC once when it first revealed it, but then reused that same DVC instance whenever it needed to edit another item on the list.

With storyboards, it appears that the a view that is revealed by a table view is typically created new each time the table view calls it up (and it doesn't call up the version of init that is in the UIViewController template). As a previous answer stated, you have to wait until "ViewDidLoad" to access any of the controls, even if you have shown this view before.

I hope this helps.




回答5:


Can you not use the setter for properties, that way you can do what you need when it's actually populated?

@IBOutlet weak var xxx:UIKitClass! {
  didSet { 
    outletSetup() 
  }
}



回答6:


Call [self baseInit] inside -(void)viewWillAppear:(BOOL)animated. myImage should have a value there.

#import "SettingItem.h"

@implementation SettingItem

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

    }
    return self;
}

-(id) initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code

    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
   [self baseInit];
}

- (void) baseInit{
    NSLog(@"myImage %@"self.myImage);
}



回答7:


in your .h file you need to replace your code:

#import <UIKit/UIKit.h>

@interface SettingItem : UIView{

}

@property (strong, nonatomic) IBOutlet UIImageView *myImage;

@end

With this code

#import <UIKit/UIKit.h>

@interface SettingItem : UIView{

  IBOutlet UIImageView*myImage;

}

@end

You are not actually telling Xcode what myImage is, you are just making it a strong, nonatomic property



来源:https://stackoverflow.com/questions/14089410/iboutlet-is-nil-inside-custom-uiview-using-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!