name of UIView that was touched

前端 未结 3 1820
生来不讨喜
生来不讨喜 2021-01-07 06:39

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

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 07:04

    "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]];
    

提交回复
热议问题