0
votes

Task is simple:

  1. We have a XIB.
  2. We have a custom UIView class implementation. Without XIB.
  3. We can add thanks to IB UIView element to the XIB and set its class to custom UIView class.
  4. We can create an outlet of our custom UIView class and connect custom UIView from XIB to it.
  5. But it seems that we should do additional steps to show our custom UIView (and its elements -- labels, images, etc) when XIB is loaded.
  6. Which steps?

UPD: The task is completed. We should use initWithCoder. Cite from Pizzaiola Gorgonzola: «initWithFrame is called when a UIView is created dynamically, from code. a view that is loaded from a .nib file is always instantiated using initWithCoder, the coder takes care of reading the settings from the .nib file». Thanks to question Subviews not showing up in UIView class.

2
Have you read the "sub classing UIView" notes in the class reference developer.apple.com/library/ios/documentation/uikit/reference/… ? You either need to add your buttons, labels etc programmatically via addSubView or load the view from a storyboard or XIB. You can also implement drawRect if you aren't using standard controls. Typically you wouldn't subclass UIview in this way - you would use a UIViewController subclass driven from a XIB or storyboardPaulw11
Thanks for reply. One moment. Assume that we have UIView which already addSubView elements. Since we can see that app shows UIView from XIB, we can conclude that IB generates code and addSubView of our custom UIView for us. But why we can not see an elements inside? It is like simple chain: A->B->C. Now C shows B, but not A inside. Strange.pmor

2 Answers

0
votes

You may do something like this:

//MYCustomView.h
@interface MYCustomView : UIView
@end

//MYCustomView.m
@implementation MYCustomView

- (void)setup {
    // Do custom stuffs here...
}

- (id)init {
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}

@end
2
votes

If those elements are not in the nib/xib, you can handle adding / creating them in initWithCoder: