9

I'm trying to create a custom UIView that brings in it's view from a nib file.

In my controller I have something like:

self.arcView=[[JtView alloc] initWithCoder:self];
self.arcView.backgroundColor=[UIColor redColor];
self.arcView.frame=CGRectMake(30.0f,200.0f, 100.0f, 100.0f);
[self.view addSubview:self.arcView];

My first question is what should go into the argument for initWithCoder (NSCoder *)? I tried self but got an incompatible pointer type but this seemed to work. But on to question #2:

Second, the argument is that you use initWithCoder with nibs and initWithFrame when putting your custom view in a frame. Well, I want to load a nib in my custom view and then put it into a frame. Can I just add a frame as above and it's ok (it looks like it works)?

1
  • Take a look here please and see the code sample stackoverflow.com/a/17798646/381807 – nesimtunc Commented Jul 22, 2013 at 22:38

4 Answers 4

9

initWithCoder is called much before init and viewDidLoad methods. And you never call it. It gets called as you load a nib file from your mainBundle.

However, It receives NSCoder as an argument. Check how it is called in a class:

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

- (void)baseClassInit {

    //initialize all ivars and properties    
}
Sign up to request clarification or add additional context in comments.

Comments

8

You are doing it the other way around: it's not you who should call initWithCoder:, it's the implementation of the loadNibNamed:owner: method that does it.

What you need to do in your code is calling

UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"theNIB" 
                                              owner:self 
                                            options:nil] objectAtIndex:0];

This would unbundle the NIB, and call your initWithCoder: initializer, and give you back a view with all the outlets connected.

2 Comments

Doesn't this method return an NSArray of all the top level objects in the NIB? What if the UIView is not first?
thx, working through this right now. I've asked another question (sorry for spamming but feel like I have to understand this).
1
self.arcView = [[[NSBundle mainBundle] loadNibNamed:@"JtView" owner:self options:nil] objectAtIndex:0];
self.arcView.frame = CGRectMake(30.0f,200.0f, 100.0f, 100.0f);
self.arcView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.arcView];

That will work and don't call initWithCoder:.

Comments

1

Never call initWithCoder explicitly, it gets call implicitly while unarchiving archived object and initialise object ivars and properties- Archive object can be your custom model class saved in persistence storage or a custom view loaded from xib file.

In your class it's look like you are trying to create a custom view, so load it from xib file, for reference @dasblinkenlight code is a perfect solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.