简体   繁体   中英

UIView build in Interface Builder connected to my custom UIViewController

I was trying to use IB in a slightly different way that I am use to and I can't get it working extending the normal approach I use, when dealing with IB.

Instead of making a new UIViewController and have the view XIB generated for me and everything linked together by Xcode, I would like to just build a small (320x40px) View XIB and link it to my already existing ViewController.

I start out by making a new file in Xcode, select "view XIB". I then open IB and add some labels etc. to the view and I set "Files Owner" to be my existing ViewController. In my existing ViewController I set the IBOutlets for the labels etc. I put in my view. I go back to IB and hook up the UILabels to my outlets in "Files Owner".

I would now think that I have a reference to the labels inside the XIB, in my viewController. This is not really the approach I would like, I see no need for my viewController to have a reference to labels inside my view.

How I usually do in code:

My ViewController controls a bunch of UIViews made entirely in code and who instantiate them by:

UIView *customView = [[CustomView alloc] initWithFrame:aFrame];
[customView setTag:MY_CUSTOM_VIEW];
[customView setDelegate:self];
[self.view addSubView:customView];
[customView release];

After this I would access the labels, buttons etc. from my controller by using the [(UILabel*)[[self.view viewWithTag:MY_CUSTOM_VIEW] myLabel] setText@"Hello, World"]; have my UIViewController implement what ever methods the customView protocol required.

How to get that functionality with IB

Should I first build a MyCustomView class extending the UIView class, have it hold all my IBOutlets, set MyCustomClass as files owner and then instantiate that as shown above? Is it OK to have a view act as viewController for the IB view and how would I relay actions to my "real" viewController?

What I would like to achieve is to deal with instantiating and laying out several UIViews in my UIViewControllers code, but have the freedom of designing some of these UIViews in IB.

All the info I can find is regarding the standard "build a UIViewController with a XIB for the view" or "How to build libraries of IB components".

I hope it makes sense and thanks for any help given:)

You can create whatever view structure you want in Interface Builder and then instantiate it using the UINib class. Once you create an UINib object it loads the contents from the nib and keeps them. Then, whenever you send it the instantiateWithOwner:options: message, it will instantiate the objects contained in the xib and return an array with the top level views. You can then add these views to your view hierarchy and handle them just like any other view you created programmatically.

If you keep the UINib object (as a property for example), you can instantiate the contents again and again, which allows your xib to be used like a template.

update: For a pre-iOS 4 workaround see my recent question and answer .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM