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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:11:57  来源:igfitidea点击:

How to send data back by popViewControllerAnimated for Swift?

iosswift

提问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

  1. Create protocolin ChildViewController
  2. Create delegatevariable in ChildViewController
  3. Extend ChildViewControllerprotocol in MainViewController
  4. Give reference to ChildViewControllerof MainViewControllerwhen navigate
  5. Define delegateMethod in MainViewController
  6. Then you can call delegatemethod from ChildViewController
  1. 创建protocolChildViewController
  2. 在中创建delegate变量ChildViewController
  3. 扩展ChildViewController协议MainViewController
  4. 提供参考ChildViewControllerMainViewControllernavigate
  5. 定义delegate方法MainViewController
  6. 然后你可以调用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 ViewControlleris 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 MyViewControllerin 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 就很简单

##代码##