Swift 如何知道VC2在VC1上何时被解除?

Swift 如何知道VC2在VC1上何时被解除?,swift,delegates,dismiss,presentviewcontroller,Swift,Delegates,Dismiss,Presentviewcontroller,我有ViewController1,它会转到ViewModel,然后转到Coordinator来演示ViewController2 问题是:我需要知道VC2是什么时候在VC1上被解除的 我需要做的是:当VC2被解除时,我需要从VC1重新加载我的表 我不能使用委托,因为我无法在双方之间进行沟通(因为协调人) 需要帮忙吗 添加一些代码:我的协调员: public class Coordinator: CoordinatorProtocol { public func openVC1() {

我有ViewController1,它会转到ViewModel,然后转到Coordinator来演示ViewController2

问题是:我需要知道VC2是什么时候在VC1上被解除的

我需要做的是:当VC2被解除时,我需要从VC1重新加载我的表

我不能使用委托,因为我无法在双方之间进行沟通(因为协调人)

需要帮忙吗

添加一些代码:我的协调员:

public class Coordinator: CoordinatorProtocol {

public func openVC1() {
    let viewModel = ViewModel1(coordinator: self)
    guard let VC1 = ViewControllerOne.instantiate(storyboard: storyboard, viewModel: viewModel) else {
        return
    }
    navigationController?.pushViewController(VC1, animated: true)
}

public func openVC2() {
    let viewModel = ViewModel2()
    guard let alertPriceDeleteVC = ViewControllerTwo.instantiate(storyboard: storyboard, viewModel: viewModel) else {
        return
    }
    let nav = UINavigationController(rootViewController: VC2)
    navigationController?.present(nav, animated: true, completion: nil)
}
协调者协议:

public protocol CoordinatorProtocol {
    func openVC1()
    func openVC2()
}
My ViewModel1通过coordinatorDelegate调用VC2:

func openVC2() {
    coordinator.openVC2()
}
完成ViewController2并将用户发送回执行VC1时的操作:

navigationController?.dismiss(animated: true, completion: nil)

您需要从“准备”中指定委托值。或者,如果您不想使用故事板/xib,您也可以从ViewController中使用initialize RedScreenVC(self)指定代理

import UIKit

class ViewController: UIViewController, NavDelegate {
    func navigate(text: String, isShown: Bool) {
        print("text: \(text) isShown: \(isShown)")
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "RedScreenVC") {
            let redScreenVC = segue.destination as? RedScreenVC
            redScreenVC?.delegate = self
        }
    }

    @IBAction func nextPageButtonEventLustener(_ sender: Any) {
        performSegue(withIdentifier: "RedScreenVC", sender: sender)
    }
}



import UIKit

protocol NavDelegate {
    func navigate(text: String, isShown: Bool)
}
class RedScreenVC: UIViewController {

    weak var delegate: NavDelegate?

    var redView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))

    var navigateButton: UIButton = {
        let button = UIButton(frame: CGRect(x: 200, y: 350, width: 150, height: 50))
        button.setTitle("Navigate", for: .normal)
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        button.backgroundColor = .blue
        return button
    }()

    @objc func buttonAction(){
        if self.redView.backgroundColor == .gray {
            self.redView.backgroundColor = .systemPink
        }
    self.delegate.navigate(text:"", isShown: true)

    }

    override func viewDidLoad() {
        navigateButton.layer.cornerRadius = 25
        redView.backgroundColor = UIColor.gray

        delegate.navigate(text: "Navigation Success", isShown: true)

        view.addSubview(redView)

        view.addSubview(navigateButton)
    }

}
如果您不想使用故事板

let redScreenVC = RedScreenVC()

redScreenVC.delegate = self

    class RedScreenVC: UIViewController {
      override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    init() {
        super.init(nibName: nil, bundle: nil)
        self.initialize()
    }

    func initialize() {
        self.view.backgroundColor=CustomColor.PAGE_BACKGROUND_COLOR_1
        //From here you need to create your email and password textfield
    }
}

哦,所以你问如何沟通,然后拒绝使用最常用的沟通方式?你还对自己施加了哪些限制?允许您使用通知吗?如果ViewModel保存模型数据,是否允许ViewController2向上发送更改到ViewModel,并让ViewModel向下发送到ViewController1?@matt我不会拒绝。我只是不知道如何使用委派,因为两个VCs都不沟通。不允许Im使用通知。我有两个视图模型,一个用于VC1,另一个用于VC2,我也不在它们之间进行通信。您还没有给出任何详细信息。你只是泛泛而谈,所以我只回答了泛泛而谈。你真的声称在你的整个宇宙中没有一个对象可以同时看到两个视图控制器吗?故事板上没有他们之间的分段?没有处理路由的对象?我觉得很难相信。我没有在情节提要上使用分段。我要做的是:从VC1,我转到ViewModel1上的一个方法,调用coordinatorDelegate来展示我的VC2。这个协调器委托是在我第一次启动VC1时设置的。然后我在协调器上执行:navigationController?.present(navigationController,动画:true,完成:nil)。我真的不知道如何回到VC1来重新加载我的表。很抱歉,我没有太多经验。好的,但这表明协调器代理可以同时访问VC1和VC2,因此它可以配置它们之间的协议代理关系。或者,协调器代理本身可以充当VC2的代理,并将单词传递回VC1。