我可以将 UIView 控件(例如 UILabel、UIPickerView)直接链接到模型实例变量吗?

发布于 2024-10-30 17:53:55 字数 349 浏览 1 评论 0原文

背景 - 通常,对于基本的 UIViewController,我了解如何创建标记为 IBOutlet 的实例变量,并使用 Interface Builder 将它们链接到实际的视图控件。

问题 - 如果我将模型对象传递给 UIViewController,是否有办法将视图控件(例如 UILabel、UIPickerView 等)直接链接到模型变量(即自定义类中的有效实例变量)?

这样,一旦用户更改 UI 控件,它就会立即更新模型属性。假设您已将模型作为引用传递传递给控制器​​,因此您已自动更新父级的值。

我认为这将避免 (a) 在初始化时转换模型值以设置 UI 组件,以及 (b) 在完成视图时必须从 UI 组件手动更新模型变量。

Background - Normally with basic UIViewControllers I'm across how you can create instance variables marked as IBOutlet's, and link these to the actual view controls with Interface Builder.

Question - If I pass a model object to a UIViewController, is there a way to link the View controls (e.g. UILabel, UIPickerView etc) directly to the model variables (i.e. effectively instance variables in a custom class)?

That is this way as soon as the user changes the UI control it would instantly update the model attributes. Assuming you've passed the model to the controller as pass-by-reference, therefore you've automatically updated the values for the parent as a result.

I'm thinking this would avoid having to (a) at initialisation translate the model values to set the UI components, and (b) when finishing up with the view having to manually update the model variables from the UI components.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

坚持沉默 2024-11-06 17:53:55

就像 onnoweb 状态一样,我认为没有任何方法可以自动执行此操作 - 主要是因为 Obj-C 永远不会知道您实际正在寻找什么类型的变量。但是,您可以(我强调这一点)通过让您的模型将视图作为属性来控制您的视图。

不过,考虑到 Obj-C 是建立在 MVC 架构之上的,我不建议这样做。让您的控制器完成工作。如果您正确设置触发器,这应该不是一个大问题。

Like onnoweb states, I don't think there's any way you could do this automatically - mainly because Obj-C would never know what types of variables you were actually looking for. However, you can (and I emphasis this), make your model control your view by letting your model have a view as a property.

Considering Obj-C is built on a MVC-architecture, I wouldn't advice it, though. Let your controller do the work. If you set triggers correctly, this shouldn't be a big problem.

送君千里 2024-11-06 17:53:55

我不认为 Obj-C 有属性绑定的概念。

I don't think Obj-C has the concept of property binding.

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