0

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 :

  1. I created a PageView.xib
  2. I created a PageView.m and PageView.h files
  3. In PageView.xib > Identity Inspector > Custom Class > Class, I put "PageView"
  4. I dragged a label into the .xib
  5. 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
    
  6. 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);
    }
    
  7. 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

4

1 回答 1

0

你可以在这里查看代码。我用新文件“PageViewNew”更新了代码

https://www.dropbox.com/s/eogulm5jkbsero9/losangeles.zip?dl=0

它工作正常。

于 2015-03-27T12:13:19.417 回答