views:

248

answers:

1

Newbie Cocoa Touch question:

I'm aiming for an interface with a UIPickerView and a UIButton.

The picker needs a delegate and dataSource, which I originally wrote as a separate PickerController class implementing the right protocols. However, I'm now unsure as to how to use interface builder to link everything up.

If I have a separate .xib file for the PickerController (with just a UIPickerView in it), and add the PickerController view as a subview in the main controller, the UIPickerView is displayed correctly, but the UIButton (which is in the main .xib file) is not. It's as if add the PickerController view takes over the whole window and obscures the main view.

I tried getting rid of the separate .xib file, and instead adding the UIPickerView directly into the main .xib file. However, with that configuration, I'm not sure how to set up the delegate and dataSource, as the file's owner (the main controller) is different from the <UIPickerViewDataSource, UIPickerViewDelegate> class I wrote (the PickerController).

Some help with the following would be great:

  • should I have a separate class per-UIPickerView?
  • should I have a separate .xib file per-UIPickerView?
  • if the delegate and dataSource are in a different class to the file's owner, how do I connect them?
  • can I add a subview to the main view without it interfering with / obscuring elements in the main view?
+1  A: 

It's possible to do what you want, but it falls well outside of the quickest path to get to the desired result. If you're a newbie I'd stick with the easy paths for now, because Cocoa Touch is enough of a learning curve already.

So. The quickest path would be to have your main view subclass the PickerController class. Then you can create your IB file, use your main view class as the file's owner, and connect up the data sources and delegates.

I'm guessing from your StackOverflow score that you're an experienced developer who is starting on iPhone dev. The above quickest approach will probably smell funny, because subclassing doesn't really feel right. It feels like you should be able to combine controllers, mixing them together to build a UI. And that is possible, but ends up being a lot of work and a bit of a headache if you're just starting out. I've found that the aesthetic payoff of doing a more elegant approach isn't worth the extra effort in most cases.

Chris Garrett
Thanks for that Chris - happy to take the ugly route to get things started quickly :)I was toying with that option (or even inlining code in the main controller), but I thought it couldn't be The Right Way because there'd be no way to include two independent UIPickerViews.Still, I'll ignore that bridge for now, and cross it when I come to it. Cheers!
Alabaster Codify
I would recommend against putting two picker views on one screen anyway. One picker view takes up half the screen. Instead, you can split into two separate screens, or you can have an action sheet-like UI where you click on an object and bring up a picker view just when you need to make a selection. (As in Safari). If you do this, then you can have both of your picker views in one .xib file.
Chris Garrett