views:

21

answers:

1

I'd like to add a 50x150 uiview that I will be using as a menu, and I want to overlay it on top of my uiviewcontroller. So far what I've done is declare a UIView IBOutlet in the UIViewController class file, and in that UIViewController xib file, I dragged a UIView from the library and hooked it up accordingly. Problem is its not showing up when trying to call it:

menu = [[UIView alloc] initWithFrame:CGRectMake(10,10,50,150)];
//[self.view insertSubview:menu atIndex:0];
[self.view addSubview:menu];
//[self.view bringSubviewToFront:menu];

I've tried different variations as you can see, all suggestions from other posts but I get nothing. Am I going down the wrong path here? What am i missing?

+1  A: 

Since you've hooked up the view in Interface Builder, you don't need to alloc a new one. To add it to the view controller's main view, the code should look like this:

[self.view addSubview:self.menu]; // Assuming your IBOutlet is a property called menu.

If you're just using an IBOutlet ivar (not recommended), it should look like this:

[self.view addSubview:menu];
Robot K
wow, can't believe I was so close. Okay, it makes sense now but I'm curious why you say IBOutlet isn't recommended?
Joseph Stein
Read the documentation at the "not recommended" link (http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmNibObjects.html#//apple_ref/doc/uid/TP40004998-SW2). It describes Apple's recommendations for dealing with IBOutlets and memory management. It says you should add the `IBOutlet` to the property declaration, not the ivar.
Robot K