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 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 :
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
You can check the code here. I have updated the code with a new file "PageViewNew"
https://www.dropbox.com/s/eogulm5jkbsero9/losangeles.zip?dl=0
It is working fine.