MVP Design Pattern

MVP Design Pattern in Swift

Model–view–presenter is a derivation of the model–view–controller architectural pattern. In MVP, the presenter assumes the functionality of the “middle-man”. In MVP, all presentation logic is pushed to the presenter.

The beauty of the MVP is that its easy to learn and to quickly adapt to.
The MVP separates the job of the View controller and breaks the MVC to make it easy to manage each part of your code.
People call the MVC a “Massive view controller” and that is because the View Controller holder all of our logic.
The business logic and the UI logic which leads to a huge file holding every case we have for the UI and what it should do.

To simplify the MVP allows the View to handle all UI logic “the how” and the Presenter tells the View what to show “the what” and the Model stays the same our date model.
Below you can find a simple MVP swift implementation. you can use to simplify your needs and your basic delegate functions between the Presenter and the View

/// BaseView is used to  setup the delegate with the default functions
protocol BaseView: class {
    func setSucceeded()
    func startLoading()
    func finishLoading()
    func setError()

extension BaseView {
    func setSucceeded() { }
    func startLoading() { }
    func finishLoading() { }
    func setError() { }

/// BasePresenter is used to setup the presenter with the default neeeded fucntions
protocol BasePresenter {
    associatedtype View
    var view: View? { get set }
    var isAttached: Bool { get }
    func attachView(_ view: View)
    func detachView(_ view: View)

/// Default implementation for the `isAttached()` method just checks if the `view` is non nil.
extension BasePresenter {
    var isAttached: Bool { return view != nil }

class BaseMvpController<V, P: BasePresenter>: UIViewController {
    typealias View = V
    private(set) var presenter: P!
    // MARK: - Initializers
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        presenter = createPresenter()
    override public init(nibName: String?, bundle: Bundle?) {
        super.init(nibName: nibName, bundle: bundle)
        presenter = createPresenter()
    /// Override and return a presenter in a subclass.
    func createPresenter() -> P {
        preconditionFailure("MVP method `createPresenter()` must be override in a subclass and do not call `super.createPresenter()`!")
    deinit {
        guard let view = self as? P.View else {return}
        if let presenter = presenter {

    // MARK: - Lifecycle
    override func viewWillAppear(_ animated: Bool) {
        guard let view = self as? P.View else {
            preconditionFailure("MVP ViewController must implement the view protocol `\(View.self)`!")
        if !presenter.isAttached {
    override func viewDidDisappear(_ animated: Bool) {


Related Posts


  1. sikis izle

    Way cool! Some very valid points! I appreciate you writing this write-up plus the rest of the website is very good. Myrah Brodie Harod

  2. sikis izle

    The details talked about in the report are some of the ideal offered. Madge Corbie Winthorpe

  3. sikis izle

    There is certainly a lot to know about this topic. I love all of the points you made. Fredrika Neall Aguie

  4. erotik izle

    That is really attention-grabbing, You are an excessively skilled blogger. Noami Moe Erny

  5. erotik izle

    You definitely know how to bring an issue to light and make it important. I cant believe youre not more popular because you definitely have the gift. Lanie Hamel Ranson

  6. film

    A round of applause for your article post. Really thank you! Really Cool. Allx Lauritz Aires

  7. film

    Now I am ready to do my breakfast, later than having my breakfast coming yet again to read other news. Theodosia Brade Mackler

  8. film

    Here is an excellent Blog You might Obtain Fascinating that we encourage you to visit. Sharia Irvine Harbot

  9. film

    Hello! This is kind of off topic but I need some advice from an established blog. Elisabetta Donovan Caldeira

  10. sikis izle

    Very good post. I will be facing many of these issues as well.. Lorenza Adolpho Vedetta

  11. erotik

    Right here is the perfect site for everyone who would like to find out about this topic. Daphne Bern Pettifer

  12. erotik film izle

    You have brought up a very great points, thank you for the post. Wilmette Iver Rogers

  13. erotik izle

    I really like your writing style, good info , appreciate it for posting : D. Gwenora Tomas Athalee

  14. porno

    There is certainly a lot to know about this topic. I love all of the points you have made. Sonja Pen McClary

  15. porno

    Awesome! Its truly remarkable post, I have got much clear idea concerning from this article. Siana Bertrando Row

  16. maseczki z filtrem

    I’m extremely pleased to discover this website. I wanted to thank you for ones time just for this fantastic read!! I absolutely enjoyed every part of it and i also have you bookmarked to see new stuff in your site.

  17. maseczki jednorazowe

    Thanks for the suggestions you have shared here. Something else I would like to express is that laptop or computer memory specifications generally increase along with other advances in the engineering. For instance, if new generations of processor chips are brought to the market, there is certainly usually a similar increase in the size preferences of all computer system memory and hard drive space. This is because software program operated simply by these processor chips will inevitably increase in power to take advantage of the new technology.

  18. cialis for sale

    Definitely, what a great blog and revealing posts, I definitely will bookmark your site. Best Regards!

  19. yabanci

    The post is absolutely fantastic! Lots of great info and inspiration, both of which we all need! Also like to admire the time and effort you put into your website and detailed info you offer! I will bookmark your website! Winnifred Milton Dutch

  20. erotik

    I was able to find good info from your blog posts. Lynette Krisha Delacourt

  21. bursa escort

    I quite like looking through an article that can make people think. Also, many thanks for permitting me to comment!| Zaria Shae Nobel

  22. erotik

    Hi there, constantly i used to check web site posts here early in the daylight, because i love to learn more and more. Jenine Jeremias Vergne

Leave A Reply

Your email address will not be published. Required fields are marked *