bruno
bruno

Reputation: 2169

I'm subclassing AlertController and get the error 'UIAlertController must have a title, a message or an action to display'

final class AlertViewComponent: UIAlertController {

    private(set) var model: Model!
    private let alert: UIAlertController

    init() {

        self.alert = UIAlertController(title: "abc", message: "def", preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: L10n.Generic.Label.ok, style: .default, handler: nil))

        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func configureAlert(options: AlertOption) {

        self.alert.title = self.model.title
        self.alert.message = self.model.message

        switch options {
        case let .oneOption(handler):

            alert.addAction(UIAlertAction(title: L10n.Generic.Label.ok, style: .default, handler: handler))

        case let .twoOptions(handlerYes, handlerNo):

            alert.addAction(UIAlertAction(title: L10n.Generic.Label.ok, style: .default, handler: handlerYes))
            alert.addAction(UIAlertAction(title: L10n.Generic.Label.yes, style: .default, handler: handlerNo))
        }
    }
}

extension AlertViewComponent: Component {

    enum AlertOption {

        case oneOption(handler: (UIAlertAction) -> Void)
        case twoOptions(handlerYes: (UIAlertAction) -> Void, handlerNo: (UIAlertAction) -> Void)
    }

    struct Model {

        let title: String
        let message: String

        init(title: String = "", message: String) {

            self.title = title
            self.message = message
        }
    }

    enum Configuration {

        case update(model: Model)
    }

    func render(with configuration: AlertViewComponent.Configuration) {

        switch configuration {
        case let .update(model):

            self.model = model
        }
    }
}

I initialise the alert with:

private let alertViewComponent: AlertViewComponent = {

    let alertViewComponent = AlertViewComponent()
    alertViewComponent.render(with: .update(model: AlertViewComponent.Model(title: "title", message: "message")))

    return alertViewComponent
}()

but when I present the alert, I get the error:

'UIAlertController must have a title, a message or an action to display'

Upvotes: 1

Views: 546

Answers (1)

Lou Franco
Lou Franco

Reputation: 89172

You never called your configureAlert function. Also, you are subclassing and also have an alert controller member. Which one got presented? There is no need to subclass alert controller at all -- just use your member.

Upvotes: 0

Related Questions