Reputation: 17169
Lets say I have a UIViewController subclass called ViewControllerOne
and another ViewControllerTwo
.
I know the at one point that the class I need in another method is either one of these. Is it possible to pass the class in a method, then inside that method which its received at, declare a UIViewController with whichever subclass was passed?
I want to know if its possible without just doing something like, if this = this, declare view one, else declare view two.
Thanks.
Upvotes: 0
Views: 208
Reputation: 5181
Josh, besides George's answer, if I understand you right, you should be aware about the following method that every NSObject
has:
- (BOOL)isKindOfClass:(Class)aClass
So, you can check this way
if ([myObject isKindOfClass:[ViewControllerOne class]])
{
//declare here ViewControllerOne
ViewControllerOne *myViewControllerOne = myObject;
}
else if ([myObject isKindOfClass:[ViewControllerTwo class]])
{
//declare here ViewControllerOne
ViewControllerTwo *myViewControllerOne = myObject;
}
Upvotes: 2
Reputation: 4905
Your method could be:
- (void)myMethod:(Class)class {
UIViewController *controller = [[class alloc] init];
}
Or something to that effect.
Or you could create the controller where you know what the class is and pass it into the method:
UIViewController *controller = [[MYViewController alloc] init];
[self myMethod:controller];
And then if need be inside the method:
- (void)myMethod:(UIViewController *)controller {
if ([controller isKindOfClass:[MYViewController class]]) {
// Do something
} else {
// Do something else
}
}
Hope that helps! :)
Upvotes: 4