views:

307

answers:

4

Hi, How can I get the name of the [touch view] in the touchesbegan event. So if it were UIView *aaaaaview I would get aaaaaview as the return; Thank You, nonono

+1  A: 

The touches parameter is a set of UITouch instances, which have a corresponding view property:

The value of the property is the view object in which the touch originally occurred.

UIViews don't have a specific name, but you can just check for their identity, i.e.:

if([touch view] == aaaview) {}

or use - (BOOL)isEqual:.

You could also use the tag property instead to give them meaningful identities:

enum MyViews {
    AaaView,
    // ...
};

aaaview.tag = AaaView;

// ...
if([touch view].tag == AaaView) {}

If you really need names for some reason not mentioned in the question, you could subclass UIView and introduce a name property.

Georg Fritzsche
How do I get the name f the view in which the touch originally occured
nonono
As gf and Frank explain, the view doesn't *have* a name. You can get the name of the class, but the variable name that you use when you code the app doesn't exist in the compiled code. Variable names are just an aid for us while we are programming. If you just want to identify views, the fastest way is to use the tag property, where you can store and read an integer.
Felixyz
+3  A: 

"UIView *aaaaaView" just refers to a place in memory. So if you had

UIView *aView;
UIView *anotherView;
aView = anotherView;

and the user touched a UIView, would the view's name be "aView" or "anotherView"? In particular, a UIView has no knowledge of the name of the variable you use to store it.

I think you're going to have to initialise an NSDictionary mapping names to UIViews.

Something in your -viewDidLoad like this, perhaps:

yourDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
                          @"aaaaaView", aaaaaView, @"viewTwo", viewTwo, nil];

and then in your event you can say

NSString *viewName = (NSString *) [yourDictionary objectForKey: [touch view]];
Frank Shearar
`-initWithObjectsAndKeys:` should be `nil` terminated.
KennyTM
@KennyTM thanks; amended.
Frank Shearar
A: 

Or maybe something like this:

if ([viewController.nibName isEqualToString:@"aaaaView"])
Wim Haanstra
A: 

Use the tag value of view that is best... [[touch view] tag] u will get that view tag if use sdk 3.2 then u can use api classNameBystring

jeeva