[英]Passing data while dismissing a view
I'm working on an app with two different views which can modally present a "settings" viewController with a tableView embedded in it. 我正在使用一个具有两个不同视图的应用程序,这些视图可以模态呈现一个“设置” viewController,其中嵌入了tableView。 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).
要将数据从嵌入式tableView传递到前两个视图,我将不得不在每个视图转换中使用prepareForSegue(据我所知)。 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). 您可以定义从展示的视图控制器到另一个视图控制器(通常是执行原始演示的VC)的展开序列 。 Unwind segues can be triggered from buttons much like "regular" segues, and since they're segues, they trigger
-prepareForSegue:
in the dismissing VC. 可以通过类似于“常规” segue的按钮来触发放松segue,并且由于它们是segue,因此它们会在关闭的VC中触发
-prepareForSegue:
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. 或者,您可以设置从提供的视图控制器到另一个VC的委派 。 This pattern is widely used in iOS development, and involves a few steps:
此模式已在iOS开发中广泛使用,涉及几个步骤:
settingsDidChange(_:)
. settingsDidChange(_:)
。 Make that method take an argument with the data you want to pass back. delegate
property of type SettingsDelegate?
SettingsDelegate?
的弱delegate
属性SettingsDelegate?
. self.delegate.settingsDidChange(_:)
, passing the new data. self.delegate.settingsDidChange(_:)
,传递新数据。 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. 最后,您可以使用永久性数据存储来保存设置。UserDefaults是设置数据的不错选择–它使您可以通过整个应用程序访问的方式保留键/值对信息。 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.
然后,您应用中的其他VC可以侦听此通知并根据需要进行更新。
You are correct about prepareForSegue
, you can't use it here because you need to use dismiss in order to pop the Settings viewController
off of the navigation stack. 您对
prepareForSegue
是正确的,您不能在这里使用它,因为您需要使用dismiss才能将Settings viewController
从导航堆栈中弹出。 A very simple solution for this would be to define a global variable. 一个非常简单的解决方案是定义一个全局变量。 A global variable is simply a variable that exists above the scope of any classes in your app.
全局变量只是存在于应用程序中任何类范围之外的变量。 For example, in your settings
viewController
file, you could do this. 例如,您可以在设置
viewController
文件中执行此操作。
var globalVariable: String
class Settings: ViewController{
//view controller stuff
didSelectRowAtIndexPath{
globalVariable = "Information"
}
}
Then, when you use the dismiss method, your previous viewcontroller will be able to access that variable as globalVariable
. 然后,当您使用dismiss方法时,您以前的viewcontroller将能够以
globalVariable
访问该变量。 It doesn't have to be a string, and you don't have to set it in didSelectRowAtIndexpath
, you can use it however you want. 它不必是字符串,也不必在
didSelectRowAtIndexpath
进行设置,可以根据需要使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.