Swift Protocol and Delegate movement Back through multiple classes

Josh

I have a View Controller that passes data to a second class. That class passes data to a third class. I am trying to use protocol and delegate to get information back to the original View Controller. However, I can't get that function to fire.

(VC -> Class 2 -> Class 3). That is the flow of data. What am I doing wrong?

Here is my code.

View Controller:

class ViewController: UIViewController, PostNumber {
    func printNumber(number: Int) {
        print("Number is \(number)")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        let thirdClass = ThirdClass()
        thirdClass.delegate = self
        
        let secondClass = SecondClass()
        secondClass.addNumbers(firstNumber: 10, secondNumber: 5)
        
    }
}

Second Class:

class SecondClass {
 func addNumbers(firstNumber: Int, secondNumber: Int) {
        let sum = firstNumber + secondNumber
        let thirdClass = ThirdClass()
        thirdClass.multiplyNumbersByTwo(number: sum)
    }
}

Third Class:

protocol PostNumber {
    func printNumber(number: Int)
}
class ThirdClass {
    var delegate: PostNumber?
    func multiplyNumbersByTwo(number: Int) {
        let result = number * 2
        print("test")
        delegate?.printNumber(number: result)
    }
}
Jordan

You have to initialize ThirdClass on ViewController and then pass that into SecondClass. In the updated code below , the thirdClass instance is created in the ViewController, and the same instance is passed to the secondClass. This ensures that both classes are referring to the same instance of ThirdClass, allowing the delegate callback to work correctly.

View Controller:

class ViewController: UIViewController, PostNumber {
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let thirdClass = ThirdClass()
        thirdClass.delegate = self
    
        let secondClass = SecondClass()
        secondClass.thirdClass = thirdClass
        secondClass.addNumbers(firstNumber: 10, secondNumber: 5)
    }

    func printNumber(number: Int) {
        print("Number is \(number)")
    }
}

Second Class:

class SecondClass {
    var thirdClass: ThirdClass?

    func addNumbers(firstNumber: Int, secondNumber: Int) {
        let sum = firstNumber + secondNumber
        thirdClass?.multiplyNumbersByTwo(number: sum)
    }
}

Third Class:

protocol PostNumber {
    func printNumber(number: Int)
}

class ThirdClass {
    var delegate: PostNumber?

    func multiplyNumbersByTwo(number: Int) {
        let result = number * 2
        delegate?.printNumber(number: result)
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift: XCTest delegate/protocol call backs (Unit test)

Force child classes to implement protocol swift

Swift 3 Protocol and Delegate Method?

Swift protocol to only implemented by specific classes

Swift - Delegate through Nav Controller

Swift Codable protocol… encoding / decoding NSCoding classes

Swift protocol method is not being called by the delegate

Swift - Protocol Delegate from TableView not working - Present Modally

Swift - UIView Delegate Protocol not returning value

Question about delegate and protocol in swift (with example)

How to edit and pass back cell data with delegate and protocol in swift

Swift protocol delegate returning nil

Access delegate protocol from Swift framework into Objective C

Protocol Delegate not working as expected returns nil swift

Protocol Delegate returns nil swift

Swift protocol extension with method in wrong delegate still works(?!)

Why is my simple swift delegate and protocol setup not working?

The same protocol implementation in multiple classes

Implement delegate using generic protocol in Swift

swift ios delegate runs simultaneously in multiple classes

iOS Swift - Protocol sending delegate to wrong TableViewController

@obj protocol delegate method not working in second class. Swift

Swift 2 - protocol delegate not called

Protocol and Delegate Help? Value isn't passing back

Swift Error on communication Protocol/Delegate

Calling delegate method through different classes in Swift

swift protocol delegate always return nil

iOS - Pass Data from Swift to ObjC VC using protocol and delegate

Protocol Delegate Does not Trigger Swift