0

I'm trying to call method of Class B from class A on the button tap event. But it does not work and below is my code.

// Viewcontroller
class ViewController: UIViewController {
   @IBAction func btnClicked(_ sender: Any) {
        var objA = A()
        objA.delegate?.TestA()
    }
}

// ClassA.swift
protocol TestA {
    func TestA()
}

class A {
    var delegate: TestA?
}


// ClassB.swift
class B : TestA {
    func TestA() {
        print(" Function A from b")
    }
}

When tapping a button, function TestA() does not invoke.

I even tried the code below, but it also didn't work:

var objB = B()
var objA = A()
objA.delegate = objB
5
  • 1
    You have not assigned anything to the delegate property of objA. Its nil. Thats why nothing is happening. Create object of class B. Assign it to delegate of class A object. Commented Feb 13, 2019 at 7:47
  • where is var objB = B() written?? Commented Feb 13, 2019 at 8:26
  • This answer can help you stackoverflow.com/a/40503024/2370587 Commented Feb 13, 2019 at 8:59
  • Possible duplicate of Examples of Delegates in Swift Commented Feb 13, 2019 at 9:00
  • On my machine, it works (if you set the delegate to objB). Will btnClicked be called at all? Commented Feb 13, 2019 at 9:05

3 Answers 3

1

Because you instantiate instance of Class A using

    var objA = A()

Clearly you haven't initialised delegate property in A because its optional its default value is nil

Now when you call

    objA.delegate?.TestA()

delegate is nil hence function TestA will not be called

Suggestion

Always use camelCasing for declaring names of functions. So TestA() is incorrect rather use testA()

EDIT 1:

Tested this

@IBAction func btnClicked(_ sender: Any) {
    let objA = A()
    let objB = B()
    objA.delegate = objB
    objA.delegate?.TestA()
}

This is working fine what is the issue?

Sign up to request clarification or add additional context in comments.

2 Comments

@manisha-upadhyay: Thank you for accepting the answer, what was the issue ?
The EDIT 1 above will work but does not benefit from defining the protocol TestA. This will work even when class B is not conforming to protocol TestA. See my answer bellow for explanation.
1

The objA.delegate is never assigned to an object, so it has an initial value of nil. The ? operator avoids calling a function on a nil object.

Comments

0

The answer by Sandeep Bhandari is right.

Some information for better understanding of Protocol and Delegates. TestA is a protocol and an optional var delegate is defined in class A. This setup is right. The idea behind this setup is that any user of class A, in this case class B which conforms to protocol TestA gets a callback from class A. You need to call the delegate.testA() function from within class A. The current implementation of ViewController is not at all benefiting from defining Protocol and Delegates.

To achieve proper usage, the class A cab be modified as follows:

protocol TestA {
    func testA()
}

class A {
        var delegate: TestA?
        func process() {
            // Do something and call delegate function to report it.
            delegate?.testA()
        }
    }

And modify ViewController as follows (copied class B for completeness):

class ViewController: UIViewController {
    @IBAction func btnClicked(_ sender: Any) {
        var objA = A()
        var objB = B()
        objA.delegate = objB
        objA.process()
    }
}

// ClassB.swift
class B : TestA {

    func TestA() {
        print(" Function A from b")
    }
}

Now function implemented in class B to conform to protocol TestA will be called when process() function on objA of class A is called. This is better use of Protocol and Delegate. Hope this helps.

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.