Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model View Presenter and iOS (Swift) Architecture

I've been looking into applying the Model View Presenter architecture to a new iOS project. After some reading, I found that this post had the best example. Link to raw gist of code here.

The bottom of the example has the assembly code:

// Assembling of MVP
let model = Person(firstName: "David", lastName: "Blaine")
let view = GreetingViewController()
let presenter = GreetingPresenter(view: view, person: model)
view.presenter = presenter

Furthermore, the author states:

Since we don’t want the View to know about the Model, it is not right to perform assembly in presenting view controller (which is the View), thus we have to do it somewhere else. For example, we can make the app-wide Router service which will be responsible for performing assembly and the View-to-View presentation.

My question:

  • Where should I put the assembly code?
  • Where can I find more example of an app-wide router?
like image 980
jonmecer Avatar asked Oct 31 '22 03:10

jonmecer


1 Answers

So it's not just me who was wondering about this ;)

Let me share with you my other finding regarding to MVP in iOS:

Screencast by David Gadd - It's a bit long (1h 20min) and a bit old (December 2012) but for sure worth seeing. You will see there how MVP is implemented (in AppCode) for a very small app (alongside with a pretty good description on how to write unit tests). The router in app David is creating is called ServiceLocator. Unfortunately I couldn't find the code to download for this screencast (but it is possible to create your own version of this app while watching it).

Perhaps there are other ways to create router, but this screencast helped me to understand it a bit more. I'm pretty new to MVP concept and I didn't use it in a bigger app (bigger than one-screen-let's-see-how-it's-done-app). It would be great to see how MVP was implemented in a real life app...

[EDIT]

I've just realised I didn't answer your first questions.

According to app in screencast router is a class with one class method:

+ (id)resolve:(PresenterTypeEnum)type;

In implementation of this method you will find simple switch. Basing on type send in a parameter method will return proper instance of a presenter.

This method is called in viewDidLoad. Once you have instance of a presenter you just have to set the view of a presenter with self.

I hope this explanation is clear. Anyway, I highly recommend watching screen cast, then it should be clear as a crystal ;)

like image 130
tahavath Avatar answered Nov 15 '22 06:11

tahavath