[英]iOS Development: How can I return data from a modal view?
If I have a parent view controller that displays a modal view with a textfield to collect data from the user, what's the best way to return that data to the parent view controller? 如果我有一个父视图控制器显示带有文本字段的模式视图,以从用户那里收集数据,那么将数据返回给父视图控制器的最佳方法是什么? Currently, I assign the parent view controller as a delegate that's called from the modal view when the user enters the data.
当前,我将父视图控制器分配为当用户输入数据时从模式视图中调用的委托。 Is there a simpler/better way to return data from a modal view?
有没有更简单/更好的方式从模式视图返回数据?
Thanks so much for your help! 非常感谢你的帮助!
Personally, I would have the modal view dispatch an NSNotification that passes the data. 就个人而言,我将让模式视图调度一个NSNotification来传递数据。 A delegate works too, of course.
当然,代表也可以工作。 I think that both a singleton and a delegate mean tighter coupling, but I understand that some might disagree.
我认为单身人士和代表都意味着更紧密的耦合,但我知道有些人可能会不同意。
But I do use singletons, too, if I need access to data stored centrally from many different places in an app. 但是,如果我需要访问应用程序中许多不同位置集中存储的数据,我也会使用单例。 I just wouldn't use it simply to pass data from a view to another.
我只是不会仅仅使用它来将数据从视图传递到另一个视图。
When needing to store and pass data around I usually have a singleton class that I use throughout my app. 当需要存储和传递数据时,我通常会在整个应用程序中使用一个singleton类。 This keeps things a little cleaner in separating my views from each other.
这样可以使我的视图彼此分离,从而使事情更加清晰。 Here's a simple implementation:
这是一个简单的实现:
http://www.galloway.me.uk/tutorials/singleton-classes/ http://www.galloway.me.uk/tutorials/singleton-classes/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.