How can I make general controls in Objective-C that can be used in multiple views in a storyboard?

I am just starting out with iOS development using xCode 4.2 and have discovered storyboards. They seem great for rapid prototyping.

I am wondering how I can create my own custom control that can be used in multiple views.

For example, let's say I want to create a regular title bar that is common to each view. I think I would have to define this control with its own controller and create an instance from several views. I just don’t know how the textbooks and questions that I have read so far do not address this.

Any tips?

+4
source share
1 answer

I found one way to do this to take the following steps:

  • Create a new xib file and set Simulated Metrics to "Freeform" to allow resizing. (MyControl.xib)
  • Fill the control with the objects that I want in the control.
  • Create a UIViewController for the view. (MyViewController.h & MyViewController.m)
  • Set File Owner for MyControl.xib to the custom class MyViewController
  • In xib, which I want to include in the control, I put the UIScrollView where I need the control (regular view will work too). ( Parent.xib )
  • Create an IBOutlet for the UISCrollView into which I will ParentController control into the ParentController .
  • Create an instance of MyViewController in the ParentController .
  • In the ParentController add the MyViewController as a UISCrollView .

In code, this means

 @implementation ParentController @synthesize myScrollView; MyViewController* myController; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super init]; if(self) { myController = [[MyController alloc] initWithNibName:@"MyView" bundle:nil]; [myScrollView addSubview:myController.view]; } } 

This seems to work and allows me to separate the Control and parent implementation, but I can't help but think that there is a better way.

+7
source

All Articles