如何知道何时在VC1上关闭VC2?

问题描述 投票:0回答:1

我有一个ViewController1,它转到ViewModel,然后到协调器以显示ViewController2。

问题是:我需要知道何时在VC1上关闭VC2。

我需要做的:关闭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)
}

CoordinatorProtocol:

public protocol CoordinatorProtocol {
    func openVC1()
    func openVC2()
}

我的ViewModel1通过coordinatorDelegate调用VC2:

func openVC2() {
    coordinator.openVC2()
}

当我完成ViewController2并将用户发送回VC1时该怎么办:

navigationController?.dismiss(animated: true, completion: nil)
swift delegates dismiss presentviewcontroller
1个回答
0
投票

您需要通过准备分配委托值。或者,如果您不想使用情节提要/ xib,则可以从ViewController中分配带有初始化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)
    }

}

如果您不想使用情节提要。

让redScreenVC = RedScreenVC()

redScreenVC.delegate =自己

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
}
}
© www.soinside.com 2019 - 2024. All rights reserved.