views:

545

answers:

3

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?

A: 

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.

Steven Canfield
Sorry about the formatting - I must have screwed it up when I posted.
JoBu1324
+4  A: 
- (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 a 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"
];
Peter Hosey
I see now - everything you said makes sense - and it works if I use "id viewController = [ [ viewControllerClass alloc ] init ];" - but I get a "viewController undeclaired" error if I try "viewControllerClass *viewController = [ [ viewControllerClass alloc ] init ];" Any idea why? Or can I not use the class parameter like that?
JoBu1324
Correct. You need the name of a class there. Remember, compile time and run time are separate things; the object in the viewControllerClass variable isn't known until run time, so you can't use it to statically declare an object variable at compile time. You have to use “id viewController”.
Peter Hosey
Thanks! You did a good job laying everything out!
JoBu1324
A: 

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.

Ben Gotow