Naresh
Naresh

Reputation: 17942

Delegate function not calling

I have 2 UIViewControllers, ViewController, SecondViewController. I defined delegate function in VC, and using in Second VC. But delegate functions not calling in Second VC.

This is mu first VC code

import UIKit

//Step1:
protocol testDelegate {
    func testFunction(string1: String, string2:String)
    func math(a:Int, b:Int)
}

class ViewController: UIViewController {

    //Step2:
    var delegateVariable: testDelegate?


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

@IBAction func moveToSecondVC(_ sender: Any) {
    let nav = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as! SecondViewController
    //Step3:
    delegateVariable?.testFunction(string1: "String1", string2: "String2")
    delegateVariable?.math(a:30, b:10)
    self.navigationController?.pushViewController(nav, animated: true)
}

}

My second VC code

import UIKit

//Step4:
class SecondViewController: UIViewController , testDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    //Step5:
    let svc = ViewController()
    svc.delegateVariable = self
}

@IBAction func btn(_ sender: Any) {
    //Step5:
    let svc = ViewController()
    svc.delegateVariable = self
}

//Step6:
func testFunction(string1: String, string2: String) {
    print(string1+string2)
}
func math(a:Int, b:Int) {
    print(a+b)
    print(a-b)
    print(a*b)
}
}

Here i'm just passing small amount of data for practice, but can any one please suggest some high level delegate example tutorial links for me.

Upvotes: 0

Views: 199

Answers (2)

Mahendra
Mahendra

Reputation: 8924

in SecondViewController you are setting....

let svc = ViewController()
svc.delegateVariable = self

That just create an object of ViewController() class and then you set the delegate. So when the obj. of the scope is finished then the memory of the object will be increased automatically.

The flow should like below....

Create an object of the Viewcontroller in SecondViewController and set the delegate

let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
vc.delegateVariable = self 

Then push the view controller in to the navigation stack.

self.navigationController?.pushViewController(svc, animated: true)

Implement the delegate method of testDelegate in SecondViewController

 func testFunction(string1: String, string2: String) {
   print(string1+string2)
 } 

func math(a:Int, b:Int) {

}

EDIT

The final code of the SecondViewController Will be...

import UIKit


class SecondViewController: UIViewController , testDelegate {

override func viewDidLoad() {
  super.viewDidLoad()


 }

@IBAction func btn(_ sender: Any) {
  let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
  vc.delegateVariable = self 
  self.navigationController?.pushViewController(svc, animated: true)
}

//MARK:- TestDelegate Methods
 func testFunction(string1: String, string2: String) {
   print(string1+string2)
 }

  func math(a:Int, b:Int) {
    print(a+b)
    print(a-b)
    print(a*b)
  }
}

Upvotes: 1

Scriptable
Scriptable

Reputation: 19758

This is why nothing is happening...

let svc = ViewController()
svc.delegateVariable = self

You are creating a NEW ViewController, not using the one that is actually in use.

It does not look like you are using the delegate pattern properly. Your ViewController should not be calling code on other view controllers.

SecondViewController should "do stuff" and then let ViewController know what it has done.

For the Math function you could just use a new class (not a view controller) and create and use this as needed. You do not need a ViewController for this.

An example of using a delegate might be something like:

protocol CreateProfileDelegate: class {
   func didCreateProfile(profile: Profile?)
   func didCancelCreateProfile()
}

class ViewController: UIViewController {
    func showCreateProfile() {
       let vc = CreateProfileViewController() 
       vc.delegate = self
       present(vc, animated: true)
    }
}

extension ViewController: CreateProfileDelegate {

    func didCreateProfile(profile: Profile?) {
       // show the profile?
    }

    func didCancelCreateProfile() {
        // show an alert maybe? 
    }
}

This way the SecondViewController (CreateProfileViewController) basically tells the first that something has happened so that it can react to it.

Upvotes: 1

Related Questions