如何在解除ViewController时将数据传递给另一个控制器?

时间:2017-04-30 13:00:13

标签: ios swift uiviewcontroller storyboard uistoryboardsegue

我希望在VC解雇时将我的数据从一个ViewController传递给另一个VC。有可能吗?

我尝试了下一种方法但没有成功:

点击按钮:

self.dismiss(animated: true) { 
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "EditViewController") as! EditViewController
        controller.segueArray = [values]
    }

再次出现EditViewController时,我的segueArray就是nil

如何在解雇时将我的数据从my ViewController传递到EditViewController

4 个答案:

答案 0 :(得分:10)

将数据传回上一个视图控制器的最佳方法是通过委托...当从ViewController A转到B时,将视图控制器A作为委托传递,并在ViewController B的viewWillDisappear方法上,调用委托方法ViewController A ..协议将有助于定义委托以及之前VC要实现的所需方法。这是一个简单的例子:

传递数据的协议:

protocol isAbleToReceiveData {
  func pass(data: String)  //data: string is an example parameter
} 

Viewcontroller A:

class viewControllerA: UIViewController, isAbleToReceiveData {

  func pass(data: String) { //conforms to protocol
  // implement your own implementation
   }

  prepare(for: Segue) {
    /** code for passing data **/
    let vc2 = ViewCOntrollerB()  /
    vc2.delegate = self   //sets the delegate in the new viewcontroller 
                          //before displaying
    present(vc2)
  }
}

解雇viewcontroller:

class viewControllerB: UIViewController {

  var delegate: isAbleToReceiveData

  viewWillDisappear {
      delegate.pass(data: "someData") //call the func in the previous vc
  }
}

答案 1 :(得分:1)

在dismiss completion块中,您将创建EditViewController的新实例。我假设另一个EditViewController实例存在于导航堆栈中,您需要找到该实例&将segueArray设置为值。 您可以通过迭代导航堆栈的视图控制器来实现:

viewController.navigationController?.viewControllers.forEach({ (vc) in
    if let editVC = vc as? EditViewController {
        editVC.segueArray = ....
    }
})

但我建议使用委托模式,例如:

protocol EditViewControllerDelegate: class {
    func setSegueArray(segues: [UIStoryboardSegue])
}

在viewcontroller(只调用ViewController)中,dismiss块是,声明一个委托属性:

class ViewController: UIViewController {
    weak var delegate: EditViewControllerDelegate?
    ....
}

然后在呈现(我假设来自EditViewController)ViewController的实例时,将委托设置为:

...
if let vc = presentingViewController as? ViewController {
    vc.delegate = self
}

将EditViewController符合委托协议,如:

extension EditViewController: EditViewControllerDelegate {
    func setSegueArray(segues: [UIStoryboardSegue]) {
        // Do the data setting here eg. self.segues = segues
    }
}

答案 2 :(得分:0)

要检测何时在视图控制器上按下后退按钮,我只需使用:

override func didMove(toParentViewController parent: UIViewController?) {
    guard parent == nil else { return } // Back button pressed

    ... // Pass on the info as shown in you example
} // didMoveToParentViewController

答案 3 :(得分:0)

通用解决方案:(?Swift 5.1)

/**
 * Returns a ViewController of a class Kind
 * ## Examples:
 * UIView.vc(vcKind: CustomViewController.self) // ref to an instance of CustomViewController
 */
public static func vc<T: UIViewController>(vcKind: T.Type? = nil) -> T? {
   guard let appDelegate = UIApplication.shared.delegate, let window = appDelegate.window else { return nil }
   if let vc = window?.rootViewController as? T {
      return vc
   } else if let vc = window?.rootViewController?.presentedViewController as? T {
      return vc
   } else if let vc = window?.rootViewController?.children {
      return vc.lazy.compactMap { $0 as? T }.first
   }
   return nil
}