Adding UIPickerViews to a view with other elements
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. 开发者_JAVA百科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?
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.
精彩评论