我正在使用两个视图控制器,即StateListVC和PlayVC。在StateListVC中,我将容器视图与3个VC一起使用。点击3rd View Controller之后,我将使用委托方法访问PlayVC。我不知道该怎么做。请帮助我解决此问题。

StateListVC

class StateListVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        if isMoveToAnotherVC
        {
            let vc = self.storyboard?.instantiateViewController(withIdentifier: "PlaceVC") as! PlaceVC
            vc.delegate = self
        }

    }
}

extension StateListVC: MoveToAnotherVC {
    func moving(string: String) {
        print(string)
    }
}


ThirdVC

protocol MoveToAnotherVC {
    func moving(string: String)
}

class PlaceVC: UIViewController {

    @IBOutlet weak var tableViewPlace: UITableView!
    var delegate: MoveToAnotherVC? = nil

    var arrPlace = ["Place1", "Place2"]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

extension PlaceVC: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrPlace.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = arrPlace[indexPath.row]
        return cell
    }
}

extension PlaceVC: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        isMoveToAnotherVC = true

        print(delegate)
        guard let delegate = self.delegate else{
            print("Delegate not set")
            return
        }

        delegate.moving(string: "test")
    }
}

最佳答案

这很不错:

protocol MoveToAnotherVC: AnyObject {
    func moving(string: String)
}

class StateListVC: UIViewController, MoveToAnotherVC {

    override func viewDidLoad() {
        super.viewDidLoad()
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "PlaceVC") as! PlaceVC
        vc.delegate = self
    }

    func moving(string: String) {
        print(string)
    }

}

class PlaceVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableViewPlace: UITableView!
    weak var delegate: MoveToAnotherVC?
    var arrPlace = ["Place1", "Place2"]

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrPlace.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = arrPlace[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        delegate?.moving(string: "test")
    }

}


我将协议设为类协议,将视图控制器实例移至viewDidLoad,删除了一些(我认为)无关的解包,并将协议/委托模式简化为基础。这可行。我会将其插入到您的项目中,并逐个添加所需的内容,直到它破裂为止,因为您的问题不在此范围内。我怀疑这可能与您未在问题中包含的内容有关,但这可以回答您有关如何设置协议/委托模式的问题。

10-05 20:26
查看更多