Swift 中未调用协议委托方法 [英] Protocol Delegate Method is not called in Swift

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

问题描述

未调用协议委托方法..

Protocol delegate method is not called..

第一个视图控制器代码

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)
}

如何调用func 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 的单独实例,因此您在推送类似这样的推送对象时已经完成了委托自我

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 中未调用协议委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆