Search code examples
objective-cuiviewuiscrollviewuiimageview

how to check if the class is created by user of it is a class provided in pobjective-c API


i have a user-defined class of type CircularDynamicUIView. it is in an array the encompass several views like 'buttonviews, uiimageviews,scrollviews and others. how to programmatically within a loop to detect this user-defined class. for example: using if-statement

how to check if this class is the class created or developed by the user and not the one that already created by objective-c.


Solution

  • As @rmaddy noted in comments, you cannot explicitly differentiate between, for example, your custom CircularDynamicUIView subclass and a default UIView ... but you can evaluate in a specific order.

    Example (using your previous question):

    for (id uiComponent in uiviews) {
    
        if ([uiComponent isKindOfClass:[CircularDynamicUIView class]]) {
            NSLog(@"Yes, it is a CircularDynamicUIView");
        } else {
            NSLog(@"No, it is NOT a CircularDynamicUIView");
        }
    
        // if CircularDynamicUIView is a subclass descendant of UIView
        // this will also be true
        if ([uiComponent isKindOfClass:[UIView class]]) {
            NSLog(@"Yes, it is a UIView");
        } else {
            NSLog(@"No, it is NOT a UIView");
        }
    
    }
    

    Since both if conditions will be true, you would not want to evaluate the second if if the first test was true.

    Again, based on your previous question, you could use this approach:

    for (id uiComponent in uiviews) {
        CircularDynamicUIView *cdView;
        UIView *uiView;
        UIImageView *uiImageView;
    
        if ([uiComponent isKindOfClass:[CircularDynamicUIView class]]) {
            cdView = (CircularDynamicUIView *)uiComponent;
        } else if ([uiComponent isKindOfClass:[UIImageView class]]) {
            uiImageView = (UIImageView *)uiComponent;
        } else if ([uiComponent isKindOfClass:[UIView class]]) {
            uiView = (UIView *)uiComponent;
        }
    
    
        if (cdView) {
            // do what you want because it's a CircularDynamicUIView
            if ([cdView isHidden]) {
                // ...
            }
            // etc ...
        }
        if (uiImageView) {
            // do what you want because it's a UIImageView
            if ([uiImageView isHidden]) {
                // ...
            }
            // etc ...
        }
        if (uiView) {
            // do what you want because it's a UIView
            if ([uiView isHidden]) {
                // ...
            }
            // etc ...
        }
    }