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
if ([cls isSubclassOfClass:[UIViewController class]]) {
//Your code
}
you can use the below code.
if ([cls isKindOfClass:[UIViewController class]]) {
//your code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With