简体   繁体   English

iOS开发:如何从模式视图返回数据?

[英]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.

相关问题 iOS开发:如何缩短代码中的URL? - iOS Development: How can I shorten a URL from my code? iOS 开发:如何在将视图控制器推送到导航堆栈之前预加载它? - iOS Development: How can I preload a view controller before pushing it onto the navigation stack? iOS开发:如何使视图向右过渡而不是默认向左过渡? - iOS Development: How can I make my view transition to the right instead of to the default left? iOS开发:如何为视图中的表单控件添加/删除添加动画效果? - iOS Development: How can I animate the addition/removal of form controls in a view? 在iOS4中从后台重新唤醒视图后,如何更新视图数据? - How can I update a view's data when it is re-awakened from the background in iOS4? iOS开发:如何加强核心数据的内存使用量? - iOS Development: How can I tighten up my Core Data memory usage? 在iOS开发中,我可以读取地图路况数据吗? - In iOS development, can I read map traffic data? 如何在iOS 8中的模态视图控制器中强制纵向方向? - How do I force portrait orientation in a modal view controller in iOS 8? iOS开发:如何防止iPad在iPad模式下运行通用应用程序? - iOS Development: How can I prevent an iPad from running a universal app in iPad mode? 如何从视图返回一个或多个值? - How can I return one or more values from a View?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM