1

Protocol delegate method is not called..

First View controller code

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

}

Second view controller code

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

how to call the func didSelectData pls help me

3 Answers 3

4

As you've already used segue for navigating between views. You can also use that for this example. I give the following as a code sample so that you can track back yourself to detect issue in your code.

enter image description here

First View Controller

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

Second View Controller

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)
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

No worries, glad it helps.
I have to pass the data with first view controller to second view controller I know segue is there but I have to use protocol delegate pls help how to do
I had count of the students in First view controller I have to pass that data count to second view controller how to pass that with out segue/ user defaults I want print that count at second view controller
if I can take the instance as Global i will access the count of the student in another controller
There are various ways of passing data from 1 view controller to the other. If you don't you segue anymore, but use navigation, from the first VC you can simply do seconVC.dataList = self.dataList and navigationController?.pushViewController(secondVC, animated: true).
2

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

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)

NOTE: - EVERY OBJECT HAS ITS OWN PROPERTIES

Comments

0

First View Controller

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

Second View Controller

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.