将 UIPickerViews 添加到具有其他元素的视图
新手 Cocoa Touch 问题:
我的目标是一个带有 UIPickerView 和 UIButton 的界面。
选择器需要一个委托和数据源,我最初将其编写为实现正确协议的单独 PickerController 类。但是,我现在不确定如何使用界面生成器来链接所有内容。
如果我有一个单独的 PickerController .xib 文件(其中只有一个 UIPickerView),并将 PickerController 视图添加为主控制器中的子视图,则 UIPickerView 将正确显示,但 UIButton (位于主 .xib 中)文件)不是。就好像添加 PickerController 视图接管整个窗口并遮挡主视图。
我尝试删除单独的 .xib 文件,而是将 UIPickerView 直接添加到主 .xib 文件中。但是,使用该配置,我不确定如何设置委托和数据源,因为文件的所有者(主控制器)与我编写的
类不同( PickerController)。
以下方面的一些帮助会很棒:
- 我应该为每个 UIPickerView 有一个单独的类吗?
- 我应该为每个 UIPickerView 有一个单独的 .xib 文件吗?
- 如果委托和数据源与文件所有者位于不同的类中,我如何连接它们?
- 我可以在主视图中添加子视图而不干扰/模糊主视图中的元素吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以做你想做的事,但它远远超出了达到预期结果的最快路径。如果您是新手,我现在会坚持使用简单的路径,因为 Cocoa Touch 的学习曲线已经足够了。
所以。最快的路径是将主视图子类化为 PickerController 类。然后,您可以创建 IB 文件,使用主视图类作为文件的所有者,并连接数据源和委托。
我从您的 StackOverflow 分数猜测您是一位经验丰富的开发人员,刚开始从事 iPhone 开发。上述最快的方法可能听起来很有趣,因为子类化感觉不太对。感觉您应该能够组合控制器,将它们混合在一起来构建 UI。这是可能的,但如果您刚刚开始,最终会需要大量工作,而且会有点头疼。我发现在大多数情况下,采用更优雅的方法所带来的美学回报并不值得付出额外的努力。
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.