Monday, December 11, 2023
HomeiOS Developmentios - Delegate is nil when passing knowledge again to VC

ios – Delegate is nil when passing knowledge again to VC


Here is my instance utilization of delegate programmatically. The code passes a message from one view to different view controller. I’ve two view SendMessageController and Obtain message controller. It is passing knowledge again state of affairs.

Protocol is outlined in SendMessageController.

protocol SendMessageDelegate: AnyObject {
    func message(textual content: String)
}
ultimate class SendMessageController: UIViewController {
        
    weak var delegate: SendMessageDelegate?
    
    

    @objc func button() {
        sendMessage()  
    }

    func sendMessage() {
       let message = “Whats up World”
       delegate?.message(textual content: message)
       print("Delegate is :(delegate)")
       print("self:(self)")   
} 

In Present Message View Controller which receives message:

ultimate class ShowMessageController: UIViewController,SendMessageDelegate {
     
    let sendMessageController = SendMessageController() 
     
    override func viewDidLoad() {
        sendMessageController.delegate = self
        print("(sendMessageController)")
    }
    
    func message(textual content: String) {
        print(“The message is:(textual content)”)
             
    }       
}
  

The delegates prints nil, and reminiscence deal with of print(self) is totally different than print(“(sendMessageController)”).
I had use delegate earlier than the place reminiscence adress of SendVC have been identical, do not know what is going on right here.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments