0

The following UIResponder methods report raw touches on screen:

- touchesBegan:withEvent:
- touchesMoved:withEvent:
- touchesEnded:withEvent:

Recently I was playing with a UIView subclass that I wanted to be touch responsive.

Initially I implemented the methods above in the UIViewController that was responsible for the view in question.

However, I realised that the touch methods were being called whenever the UIViewControllers view was being touched, not the subview I wanted.

I reimplemented the methods inside a UIView subclass and everything worked as expected.

However I feel like this is violating MVC. I have control logic inside my view.

Should I keep the methods implemented inside the UIViewController instead, and somehow hit test to interpret which view was touched, or am I correct in having the methods implemented inside the UIView subClass?

I feel like the later is the lazy way out.

Thanks for your time.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • You should override it in the view. How else would your view be reusable? You don't have to implement the touch handling for `UIButton` in every view controller that has one, it responds to the touch events itself. – dan Jul 29 '16 at 15:41
  • Does this not violate MVC? – Woodstock Jul 29 '16 at 15:45
  • 3
    You could definitely do things inside the methods that violate MVC, but overriding them doesn't violate MVC in and of itself. `UIView` is a subclass of `UIResponder` for a reason. – dan Jul 29 '16 at 15:48
  • Thanks @dan, does moving the view itself violate? – Woodstock Jul 29 '16 at 16:49

1 Answers1

1

In MVC, the view is responsible for user interaction. Therefore, it makes more sense to have your UIResponder methods overridden in your UIView. To keep strictly with MVC, you should then use a delegate pattern (or some other pattern) to call control logic functions that are in your controller.

enter image description here

The above image is from (Apple's MVC documentation). The part that pertains to your question is in the upper left. The view should tell the controller it has been touched ect, and the controller should execute the logic.

The UIViewController only is able to implement these methods because it comes with a built in view. This is confusing because it means that the UIViewController itself violates MVC, but thats a more involved discussion.

CarlNathan
  • 351
  • 3
  • 8
  • interesting, thanks for the reply. So should my UiView, delegate it's update of centre point back to the UIViewController? – Woodstock Jul 30 '16 at 00:13
  • Without know the specifics, it difficult for me to give you a great answer, but probably not. You should ask yourself what the viewController really NEEDS to know. My guess it that you could pass back something more generic. That being said, I think it is difficult to build any sizable project sticking with only MVC, and sometimes for smaller projects, its not worth the time to do everything the 'right' way. Once you start building, you'll eventually start running into problems that will demonstrate why it's important to use certain design patterns, and that is a more meaningful way to learn. – CarlNathan Jul 30 '16 at 21:05