Reputation: 5823
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
Upvotes: 7
Views: 12398
Reputation: 98
This answer comes a little late, but since you want to check the class of a Class Object (not an instance of a class), the following is the appropriate test:
if(cls == [UIViewController class]) {
// Whatever ...
}
Upvotes: 0
Reputation: 13600
// Check This out
if([youViewControllerObject isKindOfClass:[UIViewController class]])
{
NSLog(@"isViewcontroller Class");
}
Upvotes: 0
Reputation: 9836
If you have a class object representing the Objective-C class to be tested then use + (BOOL)isSubclassOfClass:(Class)aClass
which returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class. (required)
if([cls isKindOfClass:[UIViewController class]])
{
}
EDIT
If you have a class object which is a subclass of—or identical to class to be tested then use + (BOOL)isSubclassOfClass:(Class)aClass
which returns a Boolean value that indicates whether the receiving class is a subclass of, or identical to, a given class.
if([cls isSubclassOfClass:[UIViewController class]])
{
}
Upvotes: 0
Reputation: 1762
you can use the below code.
if ([cls isKindOfClass:[UIViewController class]]) {
//your code
}
Upvotes: 1
Reputation: 1639
if ([cls isSubclassOfClass:[UIViewController class]]) {
//Your code
}
Upvotes: 24