ios 如何通过 popViewControllerAnimated for Swift 发回数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30618172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send data back by popViewControllerAnimated for Swift?
提问by Varis Darasirikul
I need to send some data back from secondView to First View by popView. How can i send back the data by popViewControllerAnimated?
我需要通过popView 将一些数据从secondView 发送回First View。如何通过 popViewControllerAnimated 发回数据?
Thanks!
谢谢!
回答by iDhaval
You can pass data back using delegate
您可以使用回传数据 delegate
- Create
protocol
inChildViewController
- Create
delegate
variable inChildViewController
- Extend
ChildViewController
protocol inMainViewController
- Give reference to
ChildViewController
ofMainViewController
whennavigate
- Define
delegate
Method inMainViewController
- Then you can call
delegate
method fromChildViewController
- 创建
protocol
于ChildViewController
- 在中创建
delegate
变量ChildViewController
- 扩展
ChildViewController
协议MainViewController
- 提供参考
ChildViewController
的MainViewController
时navigate
- 定义
delegate
方法MainViewController
- 然后你可以调用
delegate
方法ChildViewController
Example
例子
In ChildViewController: Write code below...
在 ChildViewController 中:在下面编写代码...
protocol ChildViewControllerDelegate
{
func childViewControllerResponse(parameter)
}
class ChildViewController:UIViewController
{
var delegate: ChildViewControllerDelegate?
....
}
In MainViewController
在主视图控制器中
// extend `delegate`
class MainViewController:UIViewController,ChildViewControllerDelegate
{
// Define Delegate Method
func childViewControllerResponse(parameter)
{
.... // self.parameter = parameter
}
}
There are two options:
有两种选择:
A) with Segue
A) 与 Segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
let goNext = segue.destinationViewController as ChildViewController
goNext.delegate = self
}
B) without Segue
B) 没有 Segue
let goNext = storyboard?.instantiateViewControllerWithIdentifier("childView") as ChildViewController
goNext.delegate = self
self.navigationController?.pushViewController(goNext, animated: true)
Method Call
方法调用
self.delegate?.childViewControllerResponse(parameter)
回答by DHEERAJ
If you want to send data by popping, you'd do something like:
如果您想通过弹出来发送数据,您可以执行以下操作:
func goToFirstViewController() {
let a = self.navigationController.viewControllers[0] as A
a.data = "data"
self.navigationController.popToRootViewControllerAnimated(true)
}
回答by itsji10dra
Extending Dheeraj's answer in case your ViewController
is not first VC in the stack, here is the solution:
如果您ViewController
不是堆栈中的第一个 VC,请扩展 Dheeraj 的答案,以下是解决方案:
func popViewController() {
guard let myVC = self.navigationController?.viewControllers.first({ class FirstNavigationController: UIViewController {
var value: String?
}
class SecondNavigationController: UIViewController, UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
guard let vc = navigationController.topViewController as? FirstNavigationController else { return }
vc.value = "Hello again"
}
}
is MyViewController }) else { return }
myVC.data = "data"
self.navigationController?.popViewController(animated: true)
}
However, this solution will break if you have 2 or more than 2 MyViewController
in the stack. So, use wisely.
但是,如果MyViewController
堆栈中有 2 个或 2 个以上,则此解决方案将中断。因此,请明智地使用。
回答by Joe Maher
Answer given here is a little complex, quite simple to just use UINavigationControllerDelegate
这里给出的答案有点复杂,只需使用UINavigationControllerDelegate 就很简单
##代码##