Protocol Delegate between XIB and View Controller

Firda Sahidi

So I have XIB View and View Controller. I want when a button in my XIB clicked (didTapTryAgain Button), the called a function from my view controller. Then I tried to create a protocol and delegate for the class. But it still won't called my function. Here's my XIB view class:

import UIKit

protocol ErrorMessageDelegate {
    func refresh(_sender: AnyObject)
}

class ErrorMessage: UIView {
    @IBOutlet weak var imageViewError: UIImageView!
    @IBOutlet weak var labelError: UILabel!
    @IBOutlet weak var buttonTryAgain: UIButton!
    static weak var shared: ErrorMessage?
    var delegate: ErrorMessageDelegate?
    static var message: String?

    override func awakeFromNib() {
        ErrorMessage.shared = self
        labelError.text = ErrorMessage.message
    }

    @IBAction func didTapTryAgain(_ sender: UIButton) {
        delegate?.refresh(_sender: buttonTryAgain)
    }
}

And here's my View Controller class:

import Foundation

class BaseViewController: UIViewController, ErrorMessageDelegate {

    func refresh(_sender: AnyObject) {
        print("I hope my function work here")
    }

    var uiView =  UIView();

    override func viewDidLoad() {
        super.viewDidLoad()
        ErrorMessage.shared?.delegate = self
    }

    func getErrorMessage(message:String) {
        super.viewDidLoad()
        Dialog.dismiss()
        ErrorMessage.message = message
        guard let viewErrorMessage = Bundle.main.loadNibNamed("ErrorMessage", owner: self, options: nil)?.first as? ErrorMessage else { return}
        self.view.addSubview(viewErrorMessage)
    }
}

I'm following this answer for my code, and it still not working. Is anyone know how to do it? Thank you!

Sh_Khan

Your problem is that you set the delegate for a shared instance here

ErrorMessage.shared?.delegate = self / here shared?. is nil

but here

guard let viewErrorMessage = Bundle.main.loadNibNamed("ErrorMessage", owner: self, options: nil)?.first as? ErrorMessage else { return}
self.view.addSubview(viewErrorMessage)

you create a separate instance and add it

You need

var viewErrorMessage:ErrorMessage! // add to the vc

viewErrorMessage = Bundle.main.loadNibNamed("ErrorMessage", owner: self, options: nil)?.first as! ErrorMessage 
viewErrorMessage.delegate = self
self.view.addSubview(viewErrorMessage)

Also completely git rid of

static weak var shared: ErrorMessage?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Protocol-Delegate pattern not notifying View Controller

Protocol delegate between view before navigation stack

navigate between xib view files

Delegate for SwiftUI View from Hosting View Controller

how to xib view controller push a storyboard view controller

View Controller not respecting XIB autoresizing mask

IBAction from Xib file to seperate View Controller

Call view controller with xib from storyboard?

Cant add new xib's to view controller

Call view controller function from delegate class

Setting First view controller in App Delegate

Passing Data from App delegate to View Controller

Swift; delegate embedded view controller and parent

View controller added in app delegate not appearing

entity is nil in my view controller but not app delegate

UIImagePickerController crashes when its delegate is not the view controller

Set Delegate From the Declaring View Controller?

Present view controller from app delegate

Call app delegate method from view controller

Updating variable of View Controller using App Delegate

Access variables from App Delegate in View Controller

How to call method in view controller in app delegate?

Passing an integer to the app delegate from a view controller

Modify App Delegate to Use Tabbed View Controller

Opening another view controller using delegate?

Encapsulation between view and controller

How to add xib view to view controller swift 3

View Controller only Protocol has no access to View Controller properties

Child View Controllers in Page View Controller Failing to Receive Delegate Calls