I created a custom UIView that I would like to use on several different view controllers in an iPhone application. Currently, I have copied a UIView that contains the controls from the nib file for the first view controller, pasted it into other view controllers, and then connected all the actions to each view controller. This works fine, but is not the optimal way I would like to use this custom view.
My user view is quite simple, it consists of some UIButtons with labels, and clicking on these buttons causes actions that change the contents of the controls in the view controller's view.
What would be a consolidation strategy for defining and using this UIView? Ideally, I would like to simply reference this user view from the nib view controller and check that my view controller responds to actions from this user view.
EDIT: OK, based on J.Biard's suggestions, I tried the following with great luck.
I created another UIView-based nib file with the contents (currently only some UIButton objects) of my reusable view and subclass of UIView.m and .h, and then set the owner class of the nib file for my newly created class name.
Then I added most of the code from J.Biard (I changed the rectangle to 50,50,100,100, at the moment I left setDelegate, since I was just trying to get it to work visually at the moment, and I found that [self.view addSubview: view ] worked much better than [self addSubView: view]) until the end of the viewDidLoad method of the first view controller that appears when the application starts.
And what I get now is my main view with a black square in it. Am I missing the exit somewhere, or is there some kind of initialization needed for initWithFrame or drawRect in a subclass of UIView?
source share