Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create reusable iOS controls?

As a new iOS developer I am trying to determine the best approach to creating reusable controls in iOS4+. For example if I wanted to create a customized UIImageView that could be used in multiple other views what is the best approach.

I have created the control including the following files:

  • MyImageViewController.h
  • MyImageViewController.m
  • MyImageViewController.nib

Assume that I want a control that utilizes a .nib file.

How would I do the following:

  1. Create an instance of the custom control in another view, programmatically and via IB?
  2. How do I structure the custom control so it can expose "events", e.g., ontouch?
  3. How do I structure the custom control so I can call actions/methods, e.g., loadImage()?

Any insights or reference appreciated. Thanks

like image 724
ChrisP Avatar asked Jun 24 '11 00:06

ChrisP


People also ask

What is reusable in Swift?

A Swift mixin to use UITableViewCells , UICollectionViewCells and UIViewControllers in a type-safe way, without the need to manipulate their String -typed reuseIdentifiers . This library also supports arbitrary UIView to be loaded via a XIB using a simple call to loadFromNib()

How do I create a custom control in Swift?

In the storyboard, open the Assistant editor; it should display ViewController. swift. To create the outlet, click the Knob and control-drag it right underneath the animateSwitch IBOutlet . Release the drag and, in the pop-up window, name the outlet knob then click Connect.


1 Answers

You probably wouldn't use a nib/xib to create a reusable view 'control'. iOS is very different than what you may be used to from, say Java or .NET. For some kinds of views, you'll create a xib and controller/class which backs it - UITableViewCell is an example of this.

I wish I could give you a step by step guide how to achieve what you're trying to do, but I can't because I don't have enough info on what you are actually trying to do. There are several great guides on the apple developer site to get you acquainted with the View/Controller/Touch event interactions, but the site is not responding right now (I assume because of the release of the WWDC videos). Speaking of, watching videos from WWDC 2010 would be another great way to learn some of what you're trying to achieve, especially the UIKit sessions.

You aren't going to create 'controls' as you're probably used to them, but custom UIView-descendant objects and custom UIViewController-descendant objects. In my projects, I have found very very few scenarios where creating a reusable UIView was of any benefit (other than UITableViewCells). The vast majority of reusable code in an iOS project is ViewControllers. You'll expose events through the use of delegation - your controller will define a delegate which another object will implement, and receive the method calls from your controller to handle whatever was delegated. Again, the Apple guides and WWDC videos explain this very well.

Here's an example of a reusable controller and it's usage:

MyEntityViewController.m

- (id)initWithEntity:(EntityFoo *)theEntity inEditMode:(BOOL)inEditMode {   self = [super initWithNibNamed:@"EntityFooView" bundle:nil];   if(self) {     //retain entity somewhere     //dont setup view in here!   }   return self; }  - (void)viewDidLoad {   //setup view in here. This ensures compatibility with standalone operation } 

RootViewController.m

- (void)viewDidLoad {   [super viewDidLoad];    EntityViewController *evc = [[EntityViewController alloc] initWithEntity:self.someEntity inEditMode:NO];   [self.view addSubview:evc.view];    //You MUST call this method, the framework will not call it on sub-controllers   [evc viewDidLoad];    self.entityViewController = evc;   [evc release]; } 

This is not a complete implementation, as your experience and framework grows, you'll find a pattern of notifying (hint hint) sub-viewcontrollers of events in the main view controller. I strongly encourage you to look at the WWDC 2011 videos if you have access to them, as there is something coming in the next OS which has bearing on this subject matter.

like image 138
RyanR Avatar answered Sep 28 '22 03:09

RyanR