在Swift中不调用Protocol Delegate Method

时间:2017-07-20 11:56:50

标签: ios swift swift3

不调用协议委托方法..

首先查看控制器代码

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请帮助我

3 个答案:

答案 0 :(得分:1)

因为您已经使用segue在视图之间导航。您也可以将此用于此示例。我将以下内容作为代码示例,以便您可以自行跟踪以检测代码中的问题。

enter image description here

第一视图控制器

import UIKit

class ViewController: UIViewController, SecondVCDelegate {
    @IBOutlet weak var label: UILabel!

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

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let secondVC = segue.destination as? SecondViewController {
            secondVC.delegate = self
        }
    }        

    func didSelectData(_ result: String) {
        label.text = result
        print(result)
    }
}

第二视图控制器

import UIKit

protocol SecondVCDelegate: class {
    func didSelectData(_ result: String)
}

class SecondViewController: UIViewController {
    @IBOutlet weak var button: UIButton!

    weak var delegate: SecondVCDelegate?

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

    @IBAction func buttonTapped(_ sender: UIButton) {
        delegate?.didSelectData("My result")
        dismiss(animated: true, completion: nil)
    }
}

答案 1 :(得分:0)

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

你正在创建一个单独的SecondViewController实例,所以你在推送像这样推送对象的时候已经完成了委托自我

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

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

答案 2 :(得分:0)

第一视图控制器

import UIKit

class ViewController: UIViewController, SecondVCDelegate {
    @IBOutlet weak var label: UILabel!

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

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let secondVC = segue.destination as? SecondViewController {
            secondVC.delegate = self
        }
    }        

    func didSelectData(_ result: String) {
        label.text = result
        print(result)
    }
}

第二个视图控制器

import UIKit

protocol SecondVCDelegate: class {
    func didSelectData(_ result: String)
}

class SecondViewController: UIViewController {
    @IBOutlet weak var button: UIButton!

    weak var delegate: SecondVCDelegate?

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

    @IBAction func buttonTapped(_ sender: UIButton) {
        delegate?.didSelectData("My result")
        dismiss(animated: true, completion: nil)
    }
}