MVP Architecture Pattern In IOS

Posted by

Introductions

MVP architecture pattern is so popular Android, They are apply for big projects easy to maintain and reuse beside. So Today We are going to learn how to integration MVP architecture into IOS Swift.

Now We need some steps to make MVP working. Let’s get started.

Download Now

1. Create new project.

  • First of all opening your Xcode and then fill up some fields in order to create new project.

 

2. Define some folders and create class.

  • In this  tutorial will be created 4 folders(service, view, model, and presenter) for each folders you need to put some classes there.
  • Assume in this guide you will have list of staff.
  • You need to create Staff.swift into model folder

     
  • Next step you need to create StaffService.swift into server folder and also generate getStaff func

     
  • Now You have a Staff class and StaffService(where to request local or global data). you will continue to create StaffView.swift. It means where to communication between StaffService and ViewController. In the Java programming you are familiar with Interface. now in Swift language. It will be defined as protocol. Take a took at code below you will understand more.

     
  • Now you already have some classes but let them work together you need to create StaffPresenter.

     
  • You need to init StaffService and StaffView. You also define getStaffList, that where to communicate StaffService and StaffView and then print data to ViewController
  • That’s all for MVP architecture pattern. final step We need to create some UI on main.storyboard. in this tutorial We have a list of staff and then show them to tableview.
  •  
  • Now You need to map UI of main.storyboard to ViewController
  • You are going to declare presenter at ViewController

  •  
  • And then call attachView let presenter can work communicate with ViewController. At a look at getStaffList func.That func will be getting data and then display to tableview
  • Before in order to work properly. You are going to extend UITableViewDataSource and UITableViewDelegate

     
  • After getStaffList func response data. You need to extra another extension in order to reload tabview

     
  • Finally command + R in order to run and see great result.

   Conclusion

By the way you can expand for more your project. That all how MVP work in IOS

               Happy coding.

 

Leave a Reply

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