关闭视图时传递数据 [英] Passing data while dismissing a view

查看:23
本文介绍了关闭视图时传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有两个不同视图的应用程序,它可以模态地呈现一个设置"视图控制器,其中嵌入了一个 tableView.要将数据从嵌入的 tableView 传递到前两个视图,我必须在每个视图转换中使用 prepareForSegue(据我了解).但是,当我进入设置"视图并点击完成"时,我想回到之前的视图.据我所知,为了做到这一点,必须摒弃这种观点.我可以在关闭视图的同时将数据从一个视图传递到另一个视图吗?如果没有,这将如何实现?

注意:请随时纠正我,我仍在开始应用程序开发,我说的某些内容可能完全错误.

解决方案

有几种方法可以实现这一点.

您可以定义一个展开转场,从您呈现的视图控制器返回到更远的另一个视图控制器(通常是进行原始演示的 VC).Unwind segues 可以像常规"segues 一样从按钮触发,并且由于它们是 segues,它们在解除 VC 中触发 -prepareForSegue: .您可以像其他方式一样使用它来推回数据.本技术说明中记录了解除转场.

<小时>

或者,您可以设置从呈现的视图控制器到另一个 VC 的委托.这种模式在 iOS 开发中被广泛使用,涉及几个步骤:

  • 为类定义一个协议(SettingsDelegate")以符合.给它一个方法——比如settingsDidChange(_:).使该方法接受一个包含您要传回的数据的参数.
  • 为您的设置控制器提供一个 SettingsDelegate? 类型的弱 delegate 属性.首次演示该 VC 时,将代理设置为演示者.
  • 使呈现的 VC 符合委托协议,并实现您认为合适的更新方法.
  • 在呈现的 VC 调用中使用 Done 按钮 self.delegate.settingsDidChange(_:),传递新数据.演示 VC 将收到此调用并在设置"关闭时更新.

委托模式在第一次设置时可能会很棘手,但随着您的进行,会变得更容易.它记录在此处.><小时>

最后,您可以使用持久性数据存储来存储设置.UserDefaults 是设置数据的不错选择——它让您以一种可在整个过程中访问的方式保存键/值对信息应用程序.在此处阅读用户默认设置.

要在用户默认值更改时更新,您可以让设置控制器在关闭时发布通知.然后,您应用中的其他 VC 可以侦听此通知并根据需要进行更新.

I'm working on an app with two different views which can modally present a "settings" viewController with a tableView embedded in it. To pass the data from the embedded tableView to the first two views I would have to use prepareForSegue in each view transition (to my understanding). However, when I'm in the Settings view and tap on "Done" I want to go back to the view I was before. In order to do this one would have to dismiss the view as far as I know. Can I pass data from one view to another while dismissing the view? If not, how would this be accomplished?

Note: Feel free to correct me, I'm still beginning app development and some things I said might be completely wrong.

解决方案

There are a few ways you can accomplish this.

You can define an unwind segue from your presented view controller back to another view controller further back (usually the VC that did the original presentation). Unwind segues can be triggered from buttons much like "regular" segues, and since they're segues, they trigger -prepareForSegue: in the dismissing VC. You can use that just like you otherwise would to push data back. Unwind segues are documented in this tech note.


Alternately, you can set up delegation from the presented view controller to another VC. This pattern is widely used in iOS development, and involves a few steps:

  • Define a protocol ("SettingsDelegate") for classes to conform to. Give it a method – something like settingsDidChange(_:). Make that method take an argument with the data you want to pass back.
  • Give your Settings controller a weak delegate property of type SettingsDelegate?. When first presenting that VC, set the delegate to the presenter.
  • Make the presenting VC conform to the delegate protocol, and implement the method to update as you see fit.
  • Have the Done button in the presented VC call self.delegate.settingsDidChange(_:), passing the new data. The presenting VC will get this call and update as Settings dismisses.

The delegation pattern can be tricky to set up the first time, but gets easier as you go. It's documented here.


Finally, you could use a persistent data store to stash settings in. UserDefaults is a good option for settings data – it lets you keep key/value pairs of information in a way that's accessible throughout your app. Read up on user defaults here.

To update when user defaults values change, you could have your Settings controller post a Notification when dismissing. Then, other VCs in your app could listen for this notification and update as needed.

这篇关于关闭视图时传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆