views:

141

answers:

3

Much I've read talks about the advantages of setting your UI up in IB. And how when the nib is "awoken" all the objects in it "come to life".

I'm experimenting with placing about 10 UIView objects into a nib that is owned by a ViewController. The UIView objects are of type MyView and are wired back to their respective properties in the ViewController. MyView is a subclass of UIView with a couple additional properties like a UIImage *image & NSNumber *value.

The nib clearly loads as I can see some other UIImageView elements that are also in the nib. But when I try to set a property in one of the 10 MyView objects I'm unable to. And in the debugger my properties for these objects are all still 0x0.

I was also under the impression that the initWithCoder method in my MyView class would fire as each of these 10 objects awoke from sleep so I could set some properties at runtime?!?

Anyone know what's going on... I'm happy to abandon IB for this but thought I'd give it a try...

+1  A: 

UIViewControllers load their views when first accessing the self.view property. Try to see if the viewDidLoad method of your controller is called.

Also, I remember reading somewhere (could be Apple documentation) that having multiple independent views loaded from a nib is not recommended, mostly because of memory usage.

Marco Mustapic
A: 

I suspect it might be that you have assigned the view to the files owner.

Open IB. In the window that has. File's Owner First Responser View

Right click on the "view" object. If it only has one option "new referencing outlet" then it means you havent wired the view into the files owner. Simply grab that little o beside this and drag it upto the files owner. Once over files owner a new menu should popup and you can should be able to select "view"

Save this and see if is now working.

Lastly check that the "File's Owner" has been set to your class, you can simply select the File's Owner and inspect this. In one of the drop downs you will be able to select the class.

IB is cool. Dont give up... Saves upto 70% of you setup code (as stated at WWDC by Apple) so stick with it.

John.

John Ballinger
+1  A: 

viewDidLoad is the first method called when all of the objects in your view have been created. initWithCoder is called before any of them are.

viewWillAppear is called after viewDidLoad, and called before anything is displayed - it gets called each time, where viewDidLoad only gets called when your view objects are created.

Kendall Helmstetter Gelner
Argh. I had all my code in initWithCoder... I'll test it in viewDidLoad tomorrow and report back... didn't realize the difference. Thought that coder was for nibs...looks like it was for newbs...
Meltemi
Coder is for nibs, but it's for doing setup before elements arrive... don't be too hard on yourself, it's natural to think of overriding init to do work.
Kendall Helmstetter Gelner
I think I've found an even better place for my setup. Apple has a habit of stashing useful categories (i assume that's what they are?) in various hard to find UIKit "addition" documents. In one I've found awakeFromNib - Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file.
Meltemi