toast
toast

Reputation: 1980

How can I make my view controller setted as it's own delegate?

I have 'helper' singleton class that needs to pop up a login, if a login is required. I want to capture the return from the login view controller, so I have implemented a protocol in my LoginViewController, and need to set my delegate in the singleton class, but when I attempt to do:

loginController.delegate = self

Xcode tries to correct it as:

loginController.delegate = self as! LoginViewControllerDelegate

The protocol in my LoginViewController.swift file:

protocol LoginViewControllerDelegate {
    func loginViewControllerDidReturn(identifier: String)
}

Any ideas?

Upvotes: 1

Views: 103

Answers (2)

Dean
Dean

Reputation: 1542

Make sure your LoginViewController definition is :

class LoginViewController: UIViewController, LoginViewControllerDelegate {
...
}

This inform the compiler that your class implement the protocol.

You can also make an extension implementing the protocol :

extension LoginViewController: LoginViewControllerDelegate {
    // Implement here the func of your protocol
}

Upvotes: 3

Johan Nordberg
Johan Nordberg

Reputation: 3777

Does 'self' conform to the LoginViewControllerDelegate protocol?

Upvotes: 1

Related Questions