Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: How to check a given Class is a kind of UIViewController class?

I am writing a method that returns a view controller instance for a given view controller class, but I need to make sure the class passed in is actually a view controller class:

- (UIViewController *)viewControllerWithClass:(Class)cls nibName:(NSString *)nibName
{
    if (cls is kind of UIViewController subclass)
        return [[[cls alloc] initWithNibNamed:nibName bundle:nil] autorelease];
    return nil;
}

I cannot compare the name of the class since cls may not be UIViewController.

edit:

Sorry I meant inside the method, how do I check if cls is a kind of UIViewController subclass

like image 487
hzxu Avatar asked Mar 19 '13 06:03

hzxu


2 Answers

if ([cls isSubclassOfClass:[UIViewController class]]) {


   //Your code

}  
like image 162
Hasintha Janka Avatar answered Nov 03 '22 14:11

Hasintha Janka


you can use the below code.

if ([cls isKindOfClass:[UIViewController class]]) {
    //your code
}
like image 1
Yashesh Avatar answered Nov 03 '22 13:11

Yashesh