A little tip on alerts with MVVM in iOS

AlertModel

Let’s create simple alert model which will be a view model of UIAlertController.

AlertBuilder

Now when we have the model we want to build it’s view representation consistently, so we create new class called AlertBulder.

Helpful protocols and extensions

Now we want all controllers marked as AlertPresentableView, to have a method that will bind to its model’s alertModel changes and show up the desired alert controller.

Usage

Now we have all the thing set up. Let me show you how to use them.
Supppose we have a view controller which we want to show alerts. The view controller will look like this:

--

--

iOS-Developer from Russia

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store