2

I've been building Swift apps where basically all the functionality is in the ViewController. I know this isn't the optimal way to do it because design patterns help you expand the app but I don't really understand them. I keep reading articles about splitting up the Model, View and Controller but the articles don't give me answers on how to do it.

Currently I'm building an app. I've made all the functions in the ViewController again but I've split the functions up between the three sections.

My whole app is split up like this:

//Model
func stopMonitoring(_ song:Song) {
    for region in locationManager.monitoredRegions {
        guard let circularRegion = region as? CLCircularRegion, circularRegion.identifier == song.identifier else { continue }
        locationManager.stopMonitoring(for: circularRegion)
    }
}

//View
func addToView(_ song:Song){
    mapView.addAnnotation(song)
    mapView.add(MKCircle(center: song.coordinate, radius: song.radius))
}

How do I go about turning this into a Model-View-Controller design?


My thoughts have been to make separate classes of each and have them always loading but I have variables in the controller class that I use in them all which seems like it wouldn't be an efficient way to go about things. I've also heard about sharedinstances but again don't know how they come into play.

2

1 Answer 1

1

You need to give more time on understanding the MVC architecture.

M = Model, this is the data object which will be used in your entire app. It's the 'Entity' which has attributes for example, Product, User, Song, etc.

V = View. In iOS apps Views are designed using Storyboards (ideally) or created at runtime using the code.

C = Controllers. Which provides the interaction between Model, UI Elements (View) & business logic. Controllers are associated to classes with Views in storyboard.

You must design a Business Layer which serves the data to controllers and populate the Models with values. I've been using this approach for years on building scalable iOS apps and helps to add features in the app very easily by maintaining the existing functionalities.

Let me know if any more help is required.

Sign up to request clarification or add additional context in comments.

3 Comments

Really appreciate this. I'm still struggling with this. What I don't seem to get is where actually the functions go. For example should the function stopMonitoring go in a completely separate class and if so what should that class be? (Also is that where I involve sharedInstance). Also for the function addToView should that stay in the controller class?
All the functions which provides interaction between the user & UI needs to be written in Controllers, also the functions which takes data from data access layer to populate the UI elements needs to be written in controllers. Any function which updates UI addToView should be written in controllers only. sharedInstance should be implemented independent of UI so Helper classes can be used there.
So I think I've already split my app up into MVC then, but I still have an absolutely massive controller class (just the view controller of my main view). Should I split this up into more controllers? And if so is there a specific way to split them, do I just build new classes and get a reference to them from other classes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.