views:

993

answers:

3

I wanted a control like the UISlider, but with two thumbs instead of one so you could specify a range instead of just one value. I read up on other people looking for the same thing and suggestions about making new controls were over my head.

So I decided to setup what I wanted in a nib and then created a view controller to handle its behavior. It actually works really well this way accept for 2 things.

1) To load my 'custom control' into another nib I need to do it through code like this...

MaxMinSliderViewController *feeMaxMinSlider = 
    [[MaxMinSliderViewController alloc] init];
[self.view addSubview:feeMaxMinSlider.view];
feeMaxMinSlider.view.frame = CGRectMake(20, 20, 280, 54);

...instead of dragging and dropping into other nibs like normal IB controls. Is there an easy way to be able to do this?

2) In all my reading about doing this, its seems as though I was supposed to (to do it the right way) subclass UIView instead of UIViewController. I couldn't setup the UIView correctly and get it working though.

Thoughts?

+3  A: 

instead of dragging and dropping into other nibs like normal IB controls. Is there an easy way to be able to do this?

Nope! There's only an incredibly hard way!

Chris McCall
Mega votes for "an incredibly hard way" HA HA Good Stuff.
Jordan
+2  A: 

Assuming you manage to convert your class into a custom UIView instead of a UIViewController you can do part of this quite easily within Interface Builder (depending upon what kind of init method your custom control has).

Within Interface Builder drag and drop a plain old UIView from the Library Window onto your view and position/size it as required.

Then with the UIView selected, switch to the Identity tab of the Inspector Window (the tab with a little (i) icon on it) and change it from UIView to the name of your custom class. This causes an instance of your custom class to be instantiated when the NIB is loaded, instead of an empty UIView.

You can then hook up the view to an IBOutlet etc as normal.

The advantage of this approach is the ability to position and size your control visually etc. It does however only appear as a rectangle, and you don't get the ability to graphically configure the additional properties of your control as you would with a UIButton or similiar control which Interface Builder has additional support for.

I find this technique great, as I'm not that much of a visual thinker and find it hard to picture in my mind how things will look when I read a section of code containing lots of frame = CGRectMake(20, 20, 280, 54) etc. Being able to do it visually with a mouse is much better for me.

Christopher Fairbairn
Thanks! I'll give it a shot.
rob5408
A: 

There is an Interface Builder Plug-in Programming guide, but it only mentions Mac OS X, not the iPhone.

Also, as of XCode 3.2.1, there is only a new project template for creating a Mac OS X IB Plug-in; there is no such project template for iPhone (Cocoa Touch). So my guess is this is not applicable for the iPhone.

Gorm