本文介绍了Swift中未调用协议委托方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未调用协议委托方法.

First View控制器代码

class ViewController: UIViewController,customDelegate {

  var seconviewcontroller : SecondViewController = SecondViewController()
  @IBOutlet weak var Label: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()

    seconviewcontroller.delegate = self
}
func didSelectData(_ result: String) {

    Label.text = result
  print("Didselect Data Call")

}

第二视图控制器代码

import UIKit
protocol customDelegate: class {
    func didSelectData(_ result: String)
}

class SecondViewController: UIViewController {
     var delegate: customDelegate?

@IBOutlet weak var secondbutton: UIButton!

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

@IBAction func secondbuttonclick(_ sender: Any) {
     let selectedItem = "naga"

     delegate?.didSelectData(selectedItem)
}

如何调用fund didSelectData请帮助我

how to call the func didSelectData pls help me

推荐答案

因此,基本上var seconviewcontroller : SecondViewController = SecondViewController()行与推送视图控制器实例不同.

So basically in line var seconviewcontroller : SecondViewController = SecondViewController() is different from your pushing view controller instance.

您要创建SecondViewController的单独实例,因此在使用类似pushs的对象进行推送时已经完成了委托自我

You are making a separate instance of SecondViewController so you have done delegate self at the time of pushing with pushes object like that

let secondVCInstance = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
secondVCInstance.delegate = self
self.navigationController?.pushViewController(secondVCInstance, animated: true)

注意:-每个对象都有其自己的属性

这篇关于Swift中未调用协议委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:04