Pass a class as a parameter?


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?




Where's the difference between setObject:forKey: and setValue:forKey: in NSMutableDictionary?

1:



Why is my UIImageView blurred?
- (id)navControllerFromView:(Class *)viewControllerClass 
It's just Class, without the asterisk.


SHowing Activity Indicator View Using Thread Shows This Error in gdb?
(Class isn't a class name; you're not passing a pointer to an instance of the Class class, you're passing a class itself.).
Unable to push notifications to iPhone in .NET - .PEM certificate problem?
rootNavController = [ self navControllerFromView:RootViewController 
You can't pass a bare class name around like this—you have to send it a message.


What's the difference between using obj-c accessors and using dot syntax?
If you actually want to pass the class somewhere, you need to send it the class message.


UIWebView scrollBy replacement
Thus:.
Hide the tab bar when displaying the ABPeoplePickerNavigationController?
rootNavController = [ self navControllerFromView:[RootViewController class]         title:@"Contact"         imageName:@"my_info.png" ]; 


Draw a shadow behind UIWebView

2:


Hey! 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..


3:


You haven't explained any problem with the code you've posted.

What doesn't work? What are you expecting to happen? The only thing I can think of so far is that you might want to pass [RootViewController class] instead of just RootViewController.

Also, your formatting is killing me..


4:


In Swift:.
func acceptingFunction(aClass: AnyClass) {     ... 

}
call with:.
acceptingFunction(UIView) 



50 out of 100 based on 25 user ratings 400 reviews

*