我有一个 UIView 子类,我在其中覆盖了多点触控处理方法。我的问题是,如果我在该 View 上使用给定的触摸执行计算,它会破坏 Model View Controller 设计模式吗?。计算完成后,我必须隐藏 View 、打印结果并更新我的核心数据模型。我对此感到困惑。
编辑:
一些代码。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// getting all the touches in the given gesture sequence
NSSet *allTouches = [event allTouches];
// checking taps count
if ([allTouches count] == 3)
{
// here I print the results and do the calculations
[self validateGestureSequenceWithTouches:allTouches];
}
// cleaning the view
NSArray *subviews = [self subviews];
for (UIView *view in subviews) {
if (![view isKindOfClass:[UINavigationBar class]])
[view removeFromSuperview];
}
}
最佳答案
为了遵守 MVC 准则,您的业务级计算需要在您的模型层中进行。然而,并非所有计算都符合“业务计算”的条件:很多时候您必须执行计算来决定 UI 事项,例如
- 单个 UI 元素的可见性
- 单个 ui 元素的大小,
- 触摸的位置和方向,
等等。如果您的计算属于业务类型,则应将其移至模型中。否则,最好将其保留在 View 或 Controller 中。
关于ios - 在 UIView 子类中执行计算会破坏 MVC 的设计模式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15694614/