Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy I cannot get correct class of a custom class through isKindOfClass?
    primarykey
    data
    text
    <p>I've created a custom class <code>AnimalView</code> which is a subclass of <code>UIView</code> containing a <code>UILabel</code> and a <code>UIImageView</code>. </p> <pre><code>@interface AnimalView : UIView { UILabel *nameLabel; UIImageView *picture; } </code></pre> <p>Then I added in several <code>AnimalView</code> onto the ViewController.view. In the <code>touchesBegan:withEvent:</code> method, I wanted to detect if the touched object is an <code>AnimalView</code> or not. Here is the code for the viewController:</p> <pre><code>@implementation AppViewController - (void)viewDidLoad { UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:... [self.view addSubview scrollview]; for (int i = 0; i&lt;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]; } } </code></pre> <p>However, nothing happened when I clicked on the animal. When I checked the class of <code>hitView</code> by <code>NSLog(@"%@", [hitView class])</code>, it always shows <code>UIView</code> instead of <code>AnimalView</code>. 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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload