1

I'm new to iOS development and I was wondering if it is at all possible to access a UIView from within a UIView.

My current setup is that I have a main UIViewController with a UIView. Within that UIView I have a smaller UIView which I use to display debugging information. What I'd like to do is to be able to somehow access that view so that I can either see/hide it when I need to or even change the background color if I am trying to debug a logic error.

I have tried to access it using self.view, but that just gives me the parent view. I've seen on various sites and tutorials that I need to add it into a subView and work from there, but when I do that the view is set as 0x0. I also saw that I need to use IBOutlet and in the IB I need to reference it somewhere, but I have no clue how to do that as I do not see my object, that I defined in my controller's header file, anywhere in the IB.

Any advice would be greatly appreciated.

5 Answers 5

4

If you want to access your second UIView through IB:

  • Make sure it's added in Interface Builder to your root view
  • create an outlet in your ViewController with @property (nonatomic, retain) IBOutlet UIView *myView (of course, synthesize it)
  • Connect the View to your newly created outlet in File's Owner in Interface Builder
  • Voila! Should work for now - just access your view with self.myView (if everything's correct, it will have non-nil value)
1
  • Perfect! Thank you so much. I didn't even know that I could do that with the File's Owner.
    – Seb
    Jan 19, 2012 at 23:36
1

The easiest way to do this is to set an IBOutlet in your ViewController's .h file and then connect that embedded view (or subview) to that IBOutlet.

Then you can set the hidden property on and off for it whenever you want to.

Check out this Apple documentation that describes how to design user interfaces with Xcode 4, paying special attention to the "Make Connections Directly with Source Code" section.

2
  • +1 Despite trying to explain it, I think the docs are just the best place to look and learn as you've pointed out. Perhaps a video might help as well.
    – Sam
    Jan 19, 2012 at 22:27
  • While a video would help a lot. It's a bit difficult to watch one in the office.
    – Seb
    Jan 19, 2012 at 23:37
1

The given answers are good, here is another approach: give the view a tag (an int, e.g. "1") and later access it like this:

UIView * viewToGet = [theMainView viewWithTag:1]

You can assign a tag in IB as well as in code.

0

Take a look at the documentation for UIView. You'll find that there are a number of methods that let you access the contents of a view. For example, there's the subviews property, which provides an array of all the view's subviews. There's also a -viewWithTag: method that'll return a pointer to the subview having the specified tag. (You can set the tag of a view to whatever you like in Interface Builder.)

If the view hierarchy is created in a .xib file or storyboard, a common way to get access to the various members of the hierarchy is to create outlets (properties tagged with IBOutlet) in your controller and connect those outlets to the appropriate views in the .xib. The outlets will then be populated with the correct view pointers when the .xib is loaded.

0

If you are creating the subviews in code, you will want to set the frame (origin & size) of the child UIView and do [self.view addSubview:myChildView]. You can use a property or an ivar to keep track of this uiview in order to reference it later to hide it.

If you are using a xib file and you define the child view inside of the xib, then yes you will most likely want to define a IBOutlet to reference the element. Something like:

@property (nonatomic, retain) IBOutlet UIView * myChildView;

inside your implementation you will want:

@implementation MyCoolViewController

@synthesize myChildView = _myChildView;

-(void) dealloc
{
   // make sure to release reference so the property gets cleaned up
   [_myChildView release], _myChildView = nil;
   [super dealloc];
}

-(void)someFunc
{
   self.myChildView.hidden = YES; // or NO
}

Inside of Interface Builder you will need to assign that outlet to some view in your Xib. Typcially you do this by selecting "Owner", and then in the Connections Inspector click on your property name and drag it to the UIView you want to assign it to.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.