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.
If you want to access your second UIView through IB:
@property (nonatomic, retain) IBOutlet UIView *myView
(of course, synthesize it) self.myView
(if everything's correct, it will have non-nil value) 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.
The given answers are good, here is another approach: give the view a tag (an int, eg "1") and later access it like this:
UIView * viewToGet = [theMainView viewWithTag:1]
You can assign a tag in IB as well as in code.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.