简体   繁体   English

关闭视图时传递数据

[英]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开发中广泛使用,涉及几个步骤:

  • Define a protocol ("SettingsDelegate") for classes to conform to. 为类定义一个协议(“ SettingsDelegate”)。 Give it a method – something like settingsDidChange(_:) . 给它一个方法–类似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? 给您的设置控制器一个类型为SettingsDelegate?的弱delegate属性SettingsDelegate? . When first presenting that VC, set the delegate to the presenter. 首次演示该VC时,请将委托设置为演示者。
  • Make the presenting VC conform to the delegate protocol, and implement the method to update as you see fit. 使呈现的VC符合委托协议,并实现您认为合适的更新方法。
  • Have the Done button in the presented VC call self.delegate.settingsDidChange(_:) , passing the new data. 在呈现的VC调用中,具有“完成”按钮self.delegate.settingsDidChange(_:) ,传递新数据。 The presenting VC will get this call and update as Settings dismisses. 主讲的VC将获得此呼叫并在“设置”关闭时进行更新。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM