tags:

views:

2083

answers:

4

I have a nib file where I have a view that contains a background image, a button and another image that covers the full screen (a shadow) that needs to be moved to the front. On the view, I'm creating child views, and after creating those and adding them using [self addView] I need to move to the front the shadow image.

I'm currently using the tag attribute to find that view, but I'm thinking there's probably a better way, by means of identifying the subviews I add in Interface Builder by some name.

I tries adding a IBOutlet to connect the subview with its parent, but it didn't work (and made no sense, since the subview is already connected to its parent in some way).

Any hint?

+2  A: 

The IBOutlets way should work, and is probably the best way to do it. Make sure you made the proper connection in Interface Builder after you declared them in the .h file.

Daniel Dickison
No luck. I add a IBOutlet on the parent view, and connect its child, but when I try to access it I get nil instead of an object.I tried both with a property and with a variable, with the same luck.
pgb
Where are your IBOutlet's declared? If you're using the one nib per controller metaphor, another way to do it is to declare IBOutlet in the files's owner (the controller) and then make sure the outlets declared in file's owner links to the items in IB. This is how I do it.
hyuan
I'm declaring the outlets on the UIView subclass I try to connect with other view, not in the controller.
pgb
If it's in the view subclass, you need to put your code in `-awakeFromNib`, not `viewDidLoad` since that's a view _controller_ method. `-initWithCoder:` will also not work since the outlets aren't established at that point.
Daniel Dickison
Great! the code in -awakeFromNib worked. Thank you!
pgb
A: 

At what point are you trying to access the subviews? If you try within init of a ViewController, the IBOutlets will be nil. The first method you can get at them is probably viewDidLoad.

The reason it does make sense to do things this way is that IBOutlets are just direct pointers to some component, even if they are already subviews of something else. Just saves a lot of hunting.

Kendall Helmstetter Gelner
I'm trying to access them on a method called from initWithCoder: in the view.
pgb
Try using viewDidLoad instead - basically it's doing the same thing, will only be called once when the view is loaded.
Kendall Helmstetter Gelner
A: 

Using the Tag is a perfectly valid way to locate specific views, so long as you're using the viewWithTag: method. If you're already using tags, there's no need to change to IBOutlets unless you just don't like calling viewWithTag:.

HitScan
+1  A: 

The iPhone does a lazy loading of view controllers. The nib might not have been loaded in initWithCoder or any init method for that matter as Kendall specified.

viewDidLoad is the preferred place to access anything from the nib if you want to access them before the view is displayed.

Hope that helps.

lostInTransit