views:

47

answers:

3

note: This is an expansion (and clarification) of a question I asked yesterday.

I am conducting a research project where I want to record all of the user's touches in the iPhone app. After the experiment, I will be able to download the data and process it in either Excel or (more likely) Matlab and determine how many times they clicked on certain buttons, when they clicked certain buttons, etc. To do this, I would need to know:

a) When they touched

b) Where they touched

c) Which view they touched

The first two are easy, but the third I am having trouble with. I know I can do this to get the reference to the UIView that was touched:

CGPoint locationPoint = [[touches anyObject] locationInView:self];
UIView* viewYouWishToObtain = [self hitTest:locationPoint withEvent:event];

However, that will just give me a pointer to the view, not the name of the view that was touched. I could assign each view a tag, but then every time I create a new view I would need to remember to tag it (or, alternatively, log the address of each view when initialized and log it when the view is touched). Subclassing UIView and adding an automatic tag isn't really an option since I'm creating other UIButtons and UISliders and would need to subclass those also, which doesn't seem like a very good solution.

Does anyone know of a clean, easy way to do this? Thanks.

+1  A: 

You could add a category to UIView which would then be inherited by all UIView descended objects, although I'm not sure its any more efficient than tagging. Since a category can override methods then you could override init methods for automatic tagging I suppose.

http://macdevelopertips.com/objective-c/objective-c-categories.html

Ben
A: 

I'm not sure what you mean by the "name" of the view. If you mean the view name in Interface Builder, I don't believe it includes that in the instantiated objects. You could use the Tag attribute which is included, but that's just a number and not a name.

Nimrod
+1  A: 

For "Which view they touched", what information do you need?

Perhaps you could use a category to add a method to UIView. This method would generate a string containing information about the view. Such as:

  • its type e.g. UIButton etc.
  • its size and position
  • the title of the view, if it has one (e.g. the button title)
  • the parent view type and title
  • other stuff e.g. is the view enabled, what state it is in. anything you like.

For example: "Type:UIButton Title:"Back" Rect:{3,5,40,25}" or some such string.

This is very clean and gives you quite a lot of information to be going with.

invariant
Cool -- I really like this idea. I'll try it out and see how it goes.
Ned