Hi, I've created a custom class AnimalView
which is a subclass of UIView
containing a UILabel
and a UIImageView
.
@interface AnimalView : UIView {
UILabel *nameLabel;
UIImageView *picture;
}
Then I added in several AnimalView
onto the ViewController.view. In the touchesBegan:withEvent:
method, I wanted to detect if the touched object is an AnimalView
or not. Here is the code for the viewController:
@implementation AppViewController
- (void)viewDidLoad {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:...
[self.view addSubview scrollview];
for (int i = 0; i<10; i++) {
AnimalView *newAnimal = [[AnimalView alloc] init];
// customization of newAnimal
[scrollview addSubview:newAnimal;
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
UIView *hitView = touch.view;
if ([hitView isKindOfClass:[AnimalView class]]) {
AnimalView *animal = (AnimalView *)hitView;
[animal doSomething];
}
}
However, nothing happened when I clicked on the animal. When I checked the class of hitView
by NSLog(@"%@", [hitView class])
, it always shows UIView
instead of AnimalView
. Is it true that the AnimalView changed to a UIView when it is added onto the ViewController? Is there any way I can get back the original class of a custom class?