如何快速实现MVC ?

时间:2022-01-07 12:59:54

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.

我一直在开发Swift应用基本上所有的功能都在ViewController中。我知道这不是最佳的方法,因为设计模式可以帮助你扩展应用程序,但我并不真正理解它们。我一直在阅读关于拆分模型、视图和控制器的文章,但是这些文章并没有给我答案。

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.

目前我正在构建一个应用。我已经在ViewController中完成了所有的功能,但是我已经将这些功能分成了三个部分。

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.

我的想法是把每个类分开,让它们总是被加载,但是我在控制器类中有变量,我在它们里面都用了,这似乎不是一个有效的方法。我也听说过共享实例,但又不知道它们是如何发挥作用的。

1 个解决方案

#1


1  

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

您需要花更多的时间来理解MVC架构。

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.

M =模型,这是将在你的整个app中使用的数据对象,它是一个具有属性的实体,例如,产品,用户,歌曲等。

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

V =视图。在iOS应用程序中,视图是使用故事板(理想情况下)设计的,或者在运行时使用代码创建的。

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

C =控制器。提供模型、UI元素(视图)和业务逻辑之间的交互。控制器与故事板中的视图关联。

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.

您必须设计一个业务层,为控制器提供数据,并使用值填充模型。多年来,我一直在使用这种方法构建可伸缩的iOS应用程序,并通过维护现有功能,帮助在应用程序中轻松添加功能。

Let me know if any more help is required.

如果需要任何帮助,请告诉我。

#1


1  

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

您需要花更多的时间来理解MVC架构。

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.

M =模型,这是将在你的整个app中使用的数据对象,它是一个具有属性的实体,例如,产品,用户,歌曲等。

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

V =视图。在iOS应用程序中,视图是使用故事板(理想情况下)设计的,或者在运行时使用代码创建的。

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

C =控制器。提供模型、UI元素(视图)和业务逻辑之间的交互。控制器与故事板中的视图关联。

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.

您必须设计一个业务层,为控制器提供数据,并使用值填充模型。多年来,我一直在使用这种方法构建可伸缩的iOS应用程序,并通过维护现有功能,帮助在应用程序中轻松添加功能。

Let me know if any more help is required.

如果需要任何帮助,请告诉我。