[英]How Pass Data back a view controller in the stack?
I've seen a lot of questions in this category but they all seem to be be answer in the same manner, which is simply passing data between viewcontrollers, which isnt what I think I'm looking for. 我已经在这个类别中看到了很多问题,但是它们似乎都以相同的方式回答,这只是在视图控制器之间传递数据,这并不是我想要的。
I have VC1 that has data stored in it, under particular circumstance the user needs to entire information in VC2. 我有在其中存储数据的VC1,在特定情况下,用户需要在VC2中获取全部信息。 I need that info to be pass down the back to VC1, lets called the data "info"
我需要将该信息传递回VC1,称为数据“ info”
I code I was hoping to use was like this. 我希望使用的代码是这样的。 I access the VC stack and assign the variables to an optional "info" variable (given at compile time the stack doesn't know what the VC type is one down on the stack.
我访问VC堆栈,并将变量分配给可选的“ info”变量(在编译时,堆栈不知道VC类型是堆栈中的哪种类型。
let nav = self.navigationController
let nextView = nav?.viewControllers[(nav?.viewControllers.count)! - 1]
****//code that isn't working
nextView.info = self.info => Value of type 'UIViewController?' has no member 'info'
****//This works fine
nav?.popViewController(animated: true)
You need to cast it 你需要投
if let let nextView = nav?.viewControllers[(nav?.viewControllers.count)! - 1] as? VCName {
nextView.info = self.info
}
It's fragile to assume that you're in a navigation stack and that the view controller one below you is the one you need to talk to. 假设您处于导航堆栈中,并且位于您下方的视图控制器就是您要与之交谈的视图控制器,这是脆弱的。
My guess is that the previous view controller pushed you, and gave you the data you're editing. 我的猜测是以前的视图控制器推动了您的工作,并为您提供了您正在编辑的数据。
It would be better/cleaner to set up a delegate protocol between the 2 view controllers, and make yourself the new view controller's delegate before pushing it. 在两个视图控制器之间设置一个委托协议,并在推送之前让自己成为新的视图控制器的委托,这样会更好/更清洁。 Then it would just send a message to it's delegate to pass the data back.
然后它将只向其委托发送一条消息以将数据传回。
nextView
is considered as it's superclass UIViewController
because the array viewControllers
from the UINavigationController
is defined like so ( [UIViewController]
). nextView
被认为是它的超类UIViewController
因为UINavigationController
viewControllers
数组是这样定义的( [UIViewController]
)。
If your VC1 is of type FirstVC
(subclass of UIViewController
), you should cast it to its actual class to be able to "see" your additional properties. 如果您的VC1是
FirstVC
类型( UIViewController
子类),则应将其FirstVC
为它的实际类,以便“看到”您的其他属性。 The same with your next view controllers. 下一个视图控制器也是如此。 So, what happens if you do this?
那么,如果执行此操作会怎样?
let nextView = nav?.viewControllers[(nav?.viewControllers.count)! - 1] as? SecondVC
Of course, assuming your nextView
is of class SecondVC
. 当然,假设您的
nextView
是SecondVC
类。 Remember that object oriented programming has polymorphism , in which all your view controllers are descendants of UIViewController
and, thus, they are a UIViewController
in general and these situations can happen. 请记住,面向对象的编程具有多态性 ,在这种情况下,所有视图控制器都是
UIViewController
后代,因此,它们通常是UIViewController
在这种情况下可能会发生。
Anyway, the correct way would be to define a protocol, make FirstVC
conform to it, pass the first view controller as a parameter to the SecondVC
and, in the second view controller, call the protocol method back to the first view controller when choosing an option. 无论如何,正确的方法是定义一个协议,使
FirstVC
符合该协议,将第一视图控制器作为参数传递给SecondVC
,然后在第二视图控制器中,当选择一个协议时,将协议方法返回给第一视图控制器。选项。 Something like: 就像是:
protocol PassDataBackProtocol {
var result: String
}
class FirstVC: UIViewController, PassDataBackProtocol {
var result: String = "" {
didSet {
// Update your view
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let secondVC = segue.destination as? SecondVC {
secondVC.firstVC = self
}
}
}
class SecondVC: UIViewController {
weak var firstVC: PassDataBackProtocol?
func didChangeValue(newValue: String) {
firstVC?.result = newValue
navigationController?.popViewController(animated: true)
}
}
If you're using pushViewController(_:animated:)
instead of segues, then set firstVC
before doing the push. 如果您使用的是
pushViewController(_:animated:)
而不是segues, firstVC
在执行推送之前设置firstVC
。
EDIT: I added the weak
property to the back pointer, since it's important to avoid retain cicles. 编辑:我将
weak
属性添加到后指针,因为避免保留子弹很重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.