【问题标题】:How to send data back by popViewControllerAnimated for Swift?如何通过 popViewController Animated for Swift 发回数据?
【发布时间】:2015-06-03 10:59:11
【问题描述】:

我需要通过 popView 将一些数据从 secondView 发送回 First View。 如何通过 popViewControllerAnimated 发回数据?

谢谢!

【问题讨论】:

标签: ios swift


【解决方案1】:

您可以使用delegate 传回数据

  1. 在ChildViewController 中创建protocol
  2. 在ChildViewController 中创建delegate 变量
  3. 在MainViewController 中扩展ChildViewController 协议
  4. navigate 时引用MainViewController 的ChildViewController
  5. 在MainViewController 中定义delegate 方法
  6. 然后你可以从ChildViewController调用delegate方法

示例

在 ChildViewController 中:在下面写代码...

protocol ChildViewControllerDelegate
{
     func childViewControllerResponse(parameter)
}

class ChildViewController:UIViewController
{
    var delegate: ChildViewControllerDelegate?
    ....
}

在 MainViewController 中

// extend `delegate`
class MainViewController:UIViewController,ChildViewControllerDelegate
{
    // Define Delegate Method
    func childViewControllerResponse(parameter)
    {
       .... // self.parameter = parameter
    }
}

有两种选择:

A) 使用 Segue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
   let goNext = segue.destinationViewController as ChildViewController
   goNext.delegate = self
}

B) 没有转场

let goNext = storyboard?.instantiateViewControllerWithIdentifier("childView") as ChildViewController
goNext.delegate = self
self.navigationController?.pushViewController(goNext, animated: true)

方法调用

self.delegate?.childViewControllerResponse(parameter)

【讨论】:

  • 这是在 Swift 中将代理与 popViewControllerAnimated 一起使用的真正方法!:stackoverflow.com/questions/39692791/…
  • 精彩内容
  • 但我不是在推送而是弹出一个视图控制器,所以我应该怎么做?
  • @DavidSeek 解决方案与您的问题有何不同?好像是重复的
  • 我不知道 2 年前发生了什么可怕的事情,xD 似乎也一样。也许有些东西不起作用。我不知道。似乎是合法的答案
【解决方案2】:

如果你想通过弹出来发送数据,你可以这样做:

func goToFirstViewController() {
  let a = self.navigationController.viewControllers[0] as A
  a.data = "data"
  self.navigationController.popToRootViewControllerAnimated(true)
}

【讨论】:

  • 如果我想弹回上一个视图而不是第一个视图,我该如何解决?
  • 您应该为此目的使用委托
  • @VarisDarasirikul self.navigationController.popToViewController(a, animated: true)
  • @VarisDarasirikul 将索引设置为您想要的视图控制器。在我的情况下,它是 1 而不是 0。让 a = self.navigationController?.viewControllers[1] as!你的PreviousViewController。很棒的答案顺便说一句:)
  • 绝对是一个很棒的答案。每天学点新东西,很聪明,谢谢!
【解决方案3】:

如果您的 ViewController 不是堆栈中的第一个 VC,则扩展 Dheeraj 的答案,这是解决方案:

func popViewController() {
  guard let myVC = self.navigationController?.viewControllers.first({ $0 is MyViewController }) else { return } 
  myVC.data = "data"
  self.navigationController?.popViewController(animated: true)
}

但是,如果堆栈中有 2 个或多于 2 个 MyViewController,此解决方案将失效。所以,明智地使用。

【讨论】:

    【解决方案4】:

    这里给出的答案有点复杂,很简单,只需使用UINavigationControllerDelegate

    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"
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多