将 UIPickerViews 添加到具有其他元素的视图

发布于 2024-09-04 08:43:24 字数 759 浏览 4 评论 0原文

新手 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉 2024-09-11 08:43:24

你可以做你想做的事,但它远远超出了达到预期结果的最快路径。如果您是新手,我现在会坚持使用简单的路径,因为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文