views:

2163

answers:

2

I have created a custom UIView that I would like to use on multiple different view controllers in my iPhone application. Currently, I have copied the UIView that contains the controls from the nib file for the first view controller, pasted it into the other view controllers, and then wired up all of the actions to each view controller. This works fine, but is not the optimal way that I would like to use this custom view.

My custom view is reasonably simple, it consists of some UIButtons with labels, and tapping on these buttons fires actions that changes the contents of controls on my view controller's view.

What would be a strategy to consolidate the definition and usage of this UIView? Ideally, I would like to just reference this custom view from the nib of view controllers, and have my view controller respond to actions from this custom view.

EDIT: OK, based on J.Biard's suggestions, I have tried the following with not much luck.

I created another UIView based nib file with the contents (for now just some UIButton objects) of my reusable view and UIView subclass .m and .h files, and then set the nib File's Owner class to my newly created class name.

Then, I added most of the code from J.Biard (I changed the rect to 50,50,100,100, left out the setDelegate out for now, as I am just trying to get it working visually for now, and i found that [self.view addSubview:view] worked much better than [self addSubView:view]) to the end of the viewDidLoad method of the first view controller that is displayed when the app fires up.

And what I get now is my main view with a black square in it. Have I missed an outlet somewhere, or is there some initialization needed in initWithFrame or drawRect in the UIView subclass?

+2  A: 

If it is very simple I would recommend that you create a subclass of UIView in code and create instances of this class (or you can use Interface Builder to create the custom UIView that is then archived into the NIB file and restored later on also using code).

Using the code solution you could create instances of your custom UIView in your controller by calling something like:

#import "MyCustomView.h"

// let the superview decide how big it should be or set it as needed.
CGRect sizeRect = CGRectMake(0,0,0,0);

// create an instance of your view   
MyCustomView *view = [MyCustomView alloc] initWithFrame:sizeRect];

// set a custom delegate on the view or set callback methods using @selector()...
[view setDelegate:self];  // self = view controller

// add the view to the controller somewhere... (eg: update CGRect above as needed)
[self addSubView:view];

// don't forget to release the view somewhere ;-)

This example assumes that you create a delegate protocol that your View Controller can respond to or you can wire up events dynamically using @selector. If you don't want to create instances of the view in code you could add a "UIView" to your NIB file and set it's class type in the inspector window (command -> 4 -> class dropdown).

If you want to do everything in interface builder you can create your custom UIView and use something like "- (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options" (see NSBundle) to load the NIB file dynamically. This presents it's own challenges though it is also feasible.

The most involved option would be to create your own xcode custom UI library / plugin so that your custom control / view could be dragged into each NIB file from the Library window like any other control shipped by Apple.

Hope this clarifies or eliminates some options for re-using controls for you.

Cheers-

J.Biard
A: 

Create your MyCustomView class and nib file.

In the nib file, set Files Owner to MyCustomView - then design your view as normal, with a top level UIView. Create an IBOutlet in MyCustomView to link to your top level UIView in your nib file.

In MyCustomView add this method:

- (BOOL) loadMyNibFile {
if (![[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil]) {
    return NO;
}
return YES;
}

In your view controllers you can use this custom view like so

- (void)viewDidLoad {

MyCustomView *customView = [[MyCustomView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[customView loadMyNibFile];
[self.view addSubview:customView.view]; //customView.view is the IBOutlet you created
    [customView release];

}

You could also create a convenience class method on MyCustomView to do this if you liked.

bandejapaisa