2016-04-25 41 views
1

我正在创建一个锻炼跟踪应用程序来学习Swift和iOS开发。自从大学以来,我还没有写出15年以上的代码,所以我对这个MVC的东西很陌生。欢迎任何指导,但具体来说,我有一个关于如何在模型和控制器之间进行通信的最佳实践的问题。我应该从模型中返回类特定的类型到我的控制器吗?

我的模型(下面的类名“Exercise”)有它自己的类型(例如“Weights”,“Reps”),并且将这些类型返回到我的控制器来构建视图感觉不对。我的直觉是对的吗?我应该抽象这个更高级别的控制器,而不是特定于类的数据结构?或者,这是完全正常的MVC?

下面是一些代码来演示我现在在做什么,感觉不对。问题的错误部分在configureView()中。

的型号:

class Exercise { 

    var name = "placeholder name" 
    var formNotes = "placeholder notes" 
    private var repLog: [repLogEntry] = [] 
    private var currentWeights = Weights(warmup25: 0, warmup50: 0, heavy: 0) 

    init() { 
     // some test data 
     name = "Squat" 
     formNotes = "Grasp the bar 1 inch outside knurling\nPull bar apart while squeezing shoulders back" 
     self.recordExercise("16-04-20", weight: 135, reps: Reps(firstSet: 10, secondSet: 8)) 
     self.recordExercise("16-04-22", weight: 135, reps: Reps(firstSet: 11, secondSet: 9)) 
     self.setWeights(135) 


    } 

    // data to be recorded 
    private struct repLogEntry { 
     var date: String 
     var weight: Int 
     var reps: Reps 

    } 

    // reps always come in pairs 
    struct Reps { 
     var firstSet: Int 
     var secondSet: Int 
    } 

    // weights of the exercise 
    struct Weights { 
     var warmup25: Int 
     var warmup50: Int 
     var heavy: Int 
    } 

    func recordExercise(date: String, weight: Int, reps: Reps) { 
     let newRepLogEntry = repLogEntry(date: date, weight: weight, reps: reps) 
     repLog.append(newRepLogEntry) 
    } 

    func setWeights(newWeight: Int) { 
     currentWeights.heavy = newWeight 
     currentWeights.warmup25 = Int(Double(newWeight) * 0.25) 
     currentWeights.warmup50 = Int(Double(newWeight) * 0.50) 
    } 

    func getCurrentWeights() -> Weights { 
     return currentWeights 
    } 

    func getLastWorkoutReps() -> Reps { 
     return repLog.last!.reps 
    } 

    func getLastWorkoutDate() -> String { 
     return repLog.last!.date 
    } 

    func getLastWorkoutWeight() -> Int { 
     return repLog.last!.weight 
    } 
} 

和控制器:

class DetailViewController: UIViewController { 

    @IBOutlet weak var detailDescriptionLabel: UILabel! 

    @IBOutlet weak var label: UILabel? 

    let Squat = Exercise() 

    var detailItem: AnyObject? { 
     didSet { 
      // Update the view. 
      self.configureView() 
     } 
    } 

    func configureView() { 
     // Update the user interface for the detail item. 
     if let detail = self.detailItem { 
      if let label = self.detailDescriptionLabel { 
       label.text = detail.valueForKey("timeStamp")!.description 
      } 
     } 

     let currentWeights = Squat.getCurrentWeights() 
     let exerciseName = Squat.name 
     let formNotes = Squat.formNotes 
     let warmup25Text = String(currentWeights.warmup25) 
     let warmup50Text = String(currentWeights.warmup50) 
     let heavyText = String(currentWeights.heavy) 
     let lastWorkoutReps = Squat.getLastWorkoutReps() 
     let lastWorkoutDate = Squat.getLastWorkoutDate() 
     let lastWorkoutWeight = Squat.getLastWorkoutWeight() 

     label?.text = "\(exerciseName)\nWarmup (25%): \(warmup25Text)\nWarmup (50%): \(warmup50Text)\nHeavy (100%): \(heavyText)\n\(lastWorkoutDate) Reps @\(lastWorkoutWeight): \(lastWorkoutReps.firstSet) and \(lastWorkoutReps.secondSet)\n\nForm Notes:\n\(formNotes)" 

    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     self.configureView() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

回答

1

对于MVC你正在做的是 '精'。您将模型,视图和控制器分开,控制器在模型和视图之间进行调解。

在MVC中,控制器通常管理视图上的所有数据布局以及视图上的所有用户交互,因此它可能会变得很大而且杂乱。有时你会将模型传递给视图并让视图进行自我配置,这使得控制器更简单,但不是很好的MVC。

您可以考虑使用MVVM - 模型视图ViewModel - 它是MVC的扩展。基本上,它需要如何将模型数据显示出控制器并将其放入ViewModel中的逻辑,该ViewModel提供给View以供其自行配置。这简化了控制器,但也将视图与模型隔离开来。

所以,你的ViewModel会有你的text = "\(exerciseName...代码,但就是这样。它只知道如何获取一个Model对象并生成应该显示的文本。它不知道它将在哪里显示。视图使用此ViewModel进行配置,因此它可以获取所需的数据,但不知道它来自哪里。 Controller是创建ViewModel并将其传递给View的中间人。

+0

因此,将这些数据结构从Model传递到Controller符合MVC。这有帮助。 – briancl

相关问题