UITableview 从另一个类添加数据
好的,我有一个 UITableView
和一个带有 UIBarButtonSystemItemAdd
的 UINavigationController
,当按下添加按钮时,它会将用户带到一个新视图一些领域。我希望能够获取用户在这些字段中输入的信息并将其显示在表视图中。我希望用户能够添加任意数量的这些项目。
现在我想起来,有点像联系人应用程序。用户填写联系人,然后当他们返回主列表时,新联系人就在那里。
最好的方法是什么?我尝试了 NSUserDefaults 和一堆数组,但这真的很混乱。谢谢。
Ok so I have a UITableView
and a UINavigationController
with a UIBarButtonSystemItemAdd
and when the add button is pushed, it takes the user to a new view with some fields. I want to be able to get the information the user puts in these fields and display it in the table view. I want the user to be able to add as many of those items as they want.
Now that I think of it, kind of like the contacts app. The user fills out a contact and then when they go back to the main list, the new contact is there.
What is the best way to do this. I tried NSUserDefaults
and a bunch of arrays
, but that got really messy. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能想到的几种方法来处理这个问题。最简单和最好的方法是使用视图控制器的模型演示,而不是推送它,就像 iPhone 上的联系人应用程序一样。这样您就可以使用 tableview 视图控制器访问所需的所有数据,而不是尝试来回传递它。一旦用户完成创建视图控制器,模型视图就会关闭,您可以从表视图中添加。开发人员文档:http://developer.apple.com/library/ios/ipad/#featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html%23//apple_ref/doc/uid/TP40007457-CH112-SW16
A few ways I can think of handling this. The easiest and best way would be to use a model presentation of your view controller instead of pushing to it, much like the contact app on the iPhone does. That way you can access all your data needed with your tableview view controller instead of trying to pass it back and forth. once the user is finished with the creation view controller the model view is dismiss and you can add from your tableview. Developer doc: http://developer.apple.com/library/ios/ipad/#featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html%23//apple_ref/doc/uid/TP40007457-CH112-SW16
您正确使用 NSUserDefaults 了吗?像这样使用。
在一个类中:
在另一个类中:
您还可以拥有 NSNotification 并传递 NSDictionary 类型:
然后您可以添加观察者并为相应的 Selector 编写方法。
Did you use NSUserDefaults properly ? Use like this.
In one class:
In another class:
You could also have NSNotification and pass an NSDictionary type:
And then you could add observers and write the method for the corresponding Selector.
最好的方式是UIController的模型呈现风格。当用户关闭视图时,您的表视图会通过委托方法收到通知,您可以在表视图中添加该数据。
现在,如果您不想使用模型视图样式,那么您可以使用 NSUserDefault。
你不必考虑它,你应该做的是首先重置 NSUserDefault。然后将所有数据保存在 NSUserDefault 中,然后从表视图中获取该数据。
您还可以做的一件事是在 Delegate 中创建一个变量并在其中保存数据并在表视图中获取它。但我不建议你这样做。
The best way is model presentation style of UIController. when user dismiss the view your table view get notified by delegate method and there you can add that data in table view.
Now if you do not want to use model view style then You can use NSUserDefault.
You do not have to think about it what u should do is that you first reset the NSUserDefault. And then save all the data in NSUserDefault and then get that data from table view.
One thing more u can do is that make a variable in Delegate and save data in that and get it in table view. but i do not recommend you to do this.