Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a class as a parameter?

I have been lead to believe that it is possible to pass a class as a method parameter, but I'm having trouble implementing the concept. Right now I have something like:

- (id)navControllerFromView:(Class *)viewControllerClass                           title:(NSString *)title                       imageName:(NSString *)imageName {     viewControllerClass *viewController = [[viewControllerClass alloc] init];     UINavigationController *thisNavController =     [[UINavigationController alloc] initWithRootViewController: viewController];      thisNavController.tabBarItem = [[UITabBarItem alloc]                                   initWithTitle: title                                           image: [UIImage imageNamed: imageName]                                             tag: 3];     return thisNavController; } 

and I call it like this:

rootNavController = [ self navControllerFromView:RootViewController     title:@"Contact"     imageName:@"my_info.png" ]; 

What's wrong with this picture?

like image 389
JoBu1324 Avatar asked Jun 23 '09 04:06

JoBu1324


People also ask

Can we pass class as a parameter?

We can pass class objects as arguments to a method in C++. Yes, you can pass the object of a class inside a method as an argument.

Can we pass class as parameter in Java?

We have a method coypObject() which accepts an object of the current class and initializes the instance variables with the variables of this object and returns it. In the main method we are instantiating the Student class and making a copy by passing it as an argument to the coypObject() method.

Can we pass class as parameter in C#?

We can pass the data to the methods in form of arguments and an object is an instance of a class that is created dynamically. The basic data types can be passed as arguments to the C# methods in the same way the object can also be passed as an argument to a method.

Can a class is passed in function?

yes of coarse you can pass classes or functions or even modules ...


2 Answers

- (id)navControllerFromView:(Class)viewControllerClass 

It's just Class, without the asterisk. (Class isn't a class name; you're not passing a pointer to an instance of the Class class, you're passing the class itself.)

rootNavController = [ self navControllerFromView:RootViewController 

You can't pass a bare class name around like this—you have to send it a message. If you actually want to pass the class somewhere, you need to send it the class message. Thus:

rootNavController = [self navControllerFromView:[RootViewController class]                                           title:@"Contact"                                       imageName:@"my_info.png" ]; 
like image 103
Peter Hosey Avatar answered Sep 22 '22 10:09

Peter Hosey


It looks like you've almost got it. You want to pass [RootViewController class] instead of RootViewController. That gives you a Class value. Also, I don't think you want your function to take a Class *, just a Class. It's not actually an Objective-C object.

like image 38
Ben Gotow Avatar answered Sep 23 '22 10:09

Ben Gotow