如何在 swift 中点击按钮时从第二个视图控制器调用第一个视图控制器函数?

How to call a first view controller function from second view controller on a button tap in swift ?

我有一个要求,我必须在点击按钮时从第二个视图控制器调用第一个视图控制器函数。

class FirstViewController: UIViewController {

    @IBAction func firstButtonPressed(_ sender: Any) {
    // Doing ABC
    }

@IBAction func showSecondVC_ sender: Any) {
        // showingSecondVC
        }

}

    class secondViewController: UIViewController {
@IBAction func SecondButtonPressed(_ sender: Any)
    // Dismiss second vc & call First View controller method so that it does ABC.
    }

我的第一个问题是我们可以直接从第二个 VC 启动第一个 VC IBAction 吗?可能吗?

我正在考虑做以下事情

class FirstViewController: UIViewController {

    @IBAction func firstButtonPressed(_ sender: Any) {
    // call DoABC
    }

func DoABC {
// Doing ABC
}

}

    class secondViewController: UIViewController {
@IBAction func SecondButtonPressed(_ sender: Any)
    // Dismiss second vc 
// Call Firstvc.DoABC ?? How to do this ??
    }  

如何从第二个 vc 调用第一个 vc 方法 ??

  1. 创建一个协议,例如,SecondViewControllerDelegate。
  2. 向该协议添加方法签名,例如 secondViewControllerDidPressButton。
  3. 向 secondViewController 添加一个变量:var delegate: SecondViewControllerDelegate
  4. 更新 firstViewController 以实施该协议。
  5. 在firstViewController的prepareForSegue中,将firstViewController指定为即将呈现的secondViewController的delegate。
  6. 更新 secondViewController 以在按下按钮时调用 self.delegate.secondViewControllerDidPressButton。

要从 First 展示你的第二个视图控制器,你可以使用 push 或 present transition。

 @IBAction func firstButtonPressed(_ sender: Any) {
    // call DoABC


//presenting VC 
let secondVC = SecondViewController() //change this to your class name
self.presentViewController(secondVC, animated: true, completion: nil)


//for push :
navigationController?.pushViewController(SecondViewController, animated: true)

}

您可以在第二个视图中相应地使用 pop/dismiss VC 来恢复第一个视图。

class secondViewController: UIViewController {

    @IBAction func SecondButtonPressed(_ sender: Any)
        // Dismiss second vc  // Call Firstvc.DoABC ?? How to do this ??
        //if you used Present in first step then use dismiss 
        self.dismissViewControllerAnimated(false, completion: nil)

    //if you used push in first step then use pop
        self.navigationController?.popViewController(animated: true) }

您可以像下面那样使用自定义委托,并在您想调用的任何地方添加函数 "presentPage"。

protocol MainDelegate {
func  presentPage(page : Int)

}

这里有几个选项:

  1. 拆分逻辑,从每个视图控制器调用相同的代码
  2. 使用闭包回调
  3. 使用委托模式作为回调方法

选项 1 - 拆分逻辑:

class FirstViewController: UIViewController {

  let abcPerformer = ABCPerformer()

  @IBAction func firstButtonPressed(_ sender: Any) {
    abcPerformer.doABC()
  }

  @IBAction func showSecondVC_ sender: Any) {
    // showingSecondVC
  }

}

class SecondViewController: UIViewController {

    let abcPerformer = ABCPerformer()

    @IBAction func SecondButtonPressed(_ sender: Any) { 
      // Dismiss second vc & call First View controller method so that it does ABC.
      abcPerformer.doABC()
    }

}

struct ABCPerformer {

  func doABC() {
    // do ABC
  }

}

选项 2 - 创建回调:

class FirstViewController: UIViewController {

  @IBAction func firstButtonPressed(_ sender: Any) {
    doABC()
  }

  @IBAction func showSecondVC_ sender: Any) {
    // showingSecondVC
    secondVC.doABC = doABC
  }

  func doABC() {
    // do ABC
  }

}

class SecondViewController: UIViewController {

    var doABC: (() -> Void)?

    @IBAction func SecondButtonPressed(_ sender: Any) { 
      // Dismiss second vc & call First View controller method so that it does ABC.
      doABC?()
    }

}

选项 3 - 使用委托:

protocol ABCProtocol {
  func doABC()
}

class FirstViewController: UIViewController, ABCProtocol {

  @IBAction func firstButtonPressed(_ sender: Any) {
    doABC()
  }

  @IBAction func showSecondVC_ sender: Any) {
    // showingSecondVC
    secondVC.delegate = self
  }

  func doABC() {
    // do ABC
  }

}

class SecondViewController: UIViewController {

    weak var delegate: ABCProtocol?

    @IBAction func SecondButtonPressed(_ sender: Any) { 
      // Dismiss second vc & call First View controller method so that it does ABC.
      delegate?.doABC()
    }

}

可能还有更多选择,但这些应该会给您足够的选择来做出决定