Search code examples
objective-cipadios4

How to check if a UIViewController is of a particular sub-class in objective c?


I want to be able to check the type of a UIViewController to see if it is of a certain type like this

c code

if (typeof(instance1) == customUIViewController) 
{
  customUIViewController test = (customViewController)instance1;

  // do more stuff
}

Solution

  • The isKindOfClass: method indicates whether an object is an instance of given class or an instance of a subclass of that class.

    if ([instance1 isKindOfClass:[CustomUIViewController class]]) {
        // code
    }
    

    If you want to check whether an object is an instance of a given class (but not an instance of a subclass of that class), use isMemberOfClass: instead.