以 Apple 的“设置”风格编辑模型

发布于 2024-09-28 23:46:49 字数 812 浏览 1 评论 0原文

我想为 iPhone 应用程序实现一些编辑机制,即我希望用户能够编辑我的模型的实例。该模型包含两种类型的数据:第一种是 NSString,用户几乎可以在其中编写他喜欢的任何内容。第二个是选择,他可以从一些给定的值中选择一个。

我的目标是调整苹果在其“设置”应用程序中使用的方式。例如,在“设置”应用程序中,Apple 提供了一个模式视图来输入邮件帐户的信息。用户必须输入名称、地址、密码等字符串,并且必须从 SMTP 服务器身份验证方法的四个选项之一中进行选择。

我必须自己实现这一切吗?只需使用 UITableViewCellStyleValue2 单元格在表格视图中呈现数据并启用编辑,我就只有添加或删除条目的机会。然而,这显然不是我想要的。

如果我确实必须自己实施,最佳实践是什么?使用 Label 和 UITextField 创建自定义单元格没有问题,但我总共需要大约 25 个这样的单元格。我发现如果单元格移出屏幕,则没有机会收到通知,那么如何最好地保存用户输入的数据?

我当前的想法是将自定义单元格中的 UITextField“绑定”到模型中的关键路径。即,我提供了更新到自定义单元格的关键路径,并从我的控制器调用 [model setValue:cell.textField.text forKeyPath:cell.keyPath]; 在收到 UITextFieldTextDidChangeNotification.那么为每次按键更新模型是一个好主意吗?我怀疑这是否表现良好。对于多项选择单元格,我将 UINavigationController 添加到我的模式视图中。

还有更好的想法吗?

I woud like to implement some editing mechanism for an iPhone application, that is I want the user to be able to edit an instance of my model. The model contains two types of data: The first is just an NSString where the user can write virtually anything he likes. The second one is a selection where he is able to pick one from some given values.

My goal is to adapt the way Apple uses in its Settings app. For example, in the Settings app Apple presents a modal view to enter the information for mail accounts. The user has to enter strings for name, address, password, ... and he has to choose from one of four options in the authentication methods of the SMTP server.

Do I have to implement this all myself? Just presenting the data in a table view using UITableViewCellStyleValue2 cells and enabling editing gives me only the chance to add or remove entries. However, this is clearly not what I want.

If I indeed do have to implement it myself, what is the best practice? Creating custom cells with a Label and a UITextField is no problem, but I would need about 25 of those cells in total. I see that there is no chance to be notified if a cell is moving off the screen, so how do I best save the data the user entered?

My current idea is to "bind" the UITextField in the custom cell to a key path in my model. I.e. I supply the key path to update to my custom cell and from my controller I call [model setValue:cell.textField.text forKeyPath:cell.keyPath]; on receiving a UITextFieldTextDidChangeNotification. So is updating the model for each key press a good idea? I doubt that this performs well. For the multiple choice cells, I added a UINavigationController to my modal view.

Are there any better ideas?

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

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

发布评论

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

评论(1

一身软味 2024-10-05 23:46:49

我最终按照我上面的建议解决了这个问题。

我展示了一个导航控制器,它显示一个表视图,该视图使用带有标签和文本字段的自定义单元格显示我的模型。所有文本字段的委托是 UITableViewController,它实现了 UITextFieldDelegate 方法 -textFieldDidBeginEditing:-textFieldDidEndEditing。我为自定义单元提供一个标签,以便我可以设置模型的相应属性(SQLite 数据库条目)。

我在 iPhone 3G 上尝试过,效果相当不错。

无论如何,谢谢!

I eventually solved the issue as I suggested myself above.

I present a navigation controller that shows a table view displaying my model using custom cells with a label and a text field. The delegate of all textFields is the UITableViewController which implements the UITextFieldDelegate methods -textFieldDidBeginEditing: and -textFieldDidEndEditing. I supply a tag to the custom cells so I can set the according property of my model (SQLite database entry).

I tried that on an iPhone 3G and it performs reasonably well.

Thanks anyway!

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