Is there a way in Objective-C to store an object/class in a variable to be passed to alloc/init somewhere else?
For example:
UIViewController = foo
foo *bar = [[foo alloc] init]
I’m trying to create a system to dynamically create navigation buttons in a separate class based on the current view controller. I can pass ‘self’ to the method, but the variable that results does not allow me to alloc/init. I could always import the .h file directly, but ideally I would like to make reusing the code as simple as possible. Maybe I’m going about this the wrong way?
Not sure to understand your question, but if you are trying to create a fresh instance of the same class as another instance, you can do something like that:
If foo isn’t declared as
id, you can even use a shortcut:[[foo.class alloc] init]or even shorter:[foo.class new].You can also use either the class object:
Or the class name: