I'm quite new in the iOS community, so this might be a dumb question. I'm trying to create a custom view with subviews that I can interact with in the code. Here's what I did :
- I created a PageView.xib
- I created a PageView.m and PageView.h files
- In PageView.xib > Identity Inspector > Custom Class > Class, I put "PageView"
- I dragged a label into the .xib
I control+dragged the label into the PageView.h, that has the code
#import <UIKit/UIKit.h> #ifndef LosAngeles_PageView_h #define LosAngeles_PageView_h @interface PageView : UIView @property (weak, nonatomic) IBOutlet UILabel *label; @end #endif
Then I tried to use this View in a ScrollView I defined in the MainViewController
- (void)viewDidLoad { [super viewDidLoad]; // Get screen dimensions CGRect fullScreenFrame = [[UIScreen mainScreen] bounds]; NSMutableArray *pages = [NSMutableArray array]; // Initialize Views for(int i = 0; i < kNumberOfPages; i++){ // Create new Frame CGRect pageFrame; // Set x offset pageFrame.origin.x = i * fullScreenFrame.size.width; pageFrame.origin.y = 0; pageFrame.size = fullScreenFrame.size; // Get "PageView" nib content NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"PageView" owner:nil options:nil]; // Create view from the nib & set its size UIView *newPage = [nibContents lastObject]; newPage.frame = pageFrame; // Add it into the Array & the ScrollView [pages addObject:newPage]; [mainScrollView addSubview:newPage]; } // Resize the content of the ScrollView (otherwise it doesn't scroll) mainScrollView.contentSize = CGSizeMake(fullScreenFrame.size.width * kNumberOfPages, fullScreenFrame.size.height); }
Tried to run but got an error
'NSUnknownKeyException', reason: '[<NSObject 0x7a7dbb70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label.'
What am I doing wrong ?
Extra informations :
- PageView.xib File's Owner shows "Label <-> label" in "Outlets" (only one here)
- the Label view shows "Label <-> File's Owner" in "Referencing Outlets" (only one here as well)
- All three files have the project ticked in the Target Membership
- Everything works fine when I simply don't reference the outlet
- Tried to clean, delete cache and other things of the kind.
Thanks for your time, don't hesitate to ask questions, and I can put the project on Github if anyone wants to see more precisely.
Julien