如何在我的代码中准确地实现 MVC 设计模式?
- Controller -> 使用 RestKit 调用 Rest 服务。
- 将 JSON 绑定(bind)到一个对象 --> 这是一个模型
- Controller 根据模型显示一堆数据。
现在我应该在哪里实现 View? 我错过了什么吗?
最佳答案
您的 ViewController 应该观察模型的变化并更新它的 View 层次结构,其根是 self.view
。
- (void)viewDidLoad {
[super viewDidLoad];
// observe the model, via kvo, or subscribe to notification, or make self == somebody's delegate, etc.
}
- (IBAction)doSomething:(id)sender {
// change the model [self.model change]
// or start a web request with self as delegate
}
// called by kvo or delegate or notification or [self modelDidChange];
- (void)modelDidChange {
// update self.view or children viewWithTag: or outlets setup to subviews
}
关于iOS Model View Controller 设计模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15283228/