Reputation: 31
I'm trying to write code that'll simplify the addition of new functions in an internal tool build. There is a UITableView with each cell being a button for a function that'll segue into another view.
Each function is in its own class file. Currently, this is the way I'm doing it.
In the view controller, I initialize the class in a method, then I segue into its view controller
- (void)resetButtonTouched {
[ResetClass *reset = [[ResetClass alloc] initWithNumber num];
[self.navigationController pushViewController: reset animated: YES];
}
Then for cellForRowAtIndexPath I create the cell
cell.textLabel.text = @"Reset";
Then in the didSelectRowAtIndexPath I create the button response
[_viewController resetButtonTouched];
And I do that for the 10 other functions, so
[Function2 *function2 = [[Function2 alloc] initWithNumber num]; [Function3 *function3 = [[Function2 alloc] initWithNumber num];
etc...
I'd like to simplify this so I only have to register a class in one place and it'll automatically add the cell and connect the button. For cellForRowAtIndexPath and didSelectRowAtIndexPath I can just get it to look in an array based on indexPath.row. But, I need a way to store the class objects in an array and initialize them when called upon.
I'm thinking I can have every function that gets added here to have class methods that'll
and a normal method to be called after initialization to do its stuff.
I want to write something where if I just write these two lines,
ResetFunction *reset;
[self registerFunction:reset];
It'll do everything I did above (create a working button that calls that class object)
I'm thinking of having an array that will store all the different class objects, call it allFunctions
, and a method runFunction
that'll do the initialization and segueing. Then in cellForRowAtIndexPath
it'll just be
cell.textLabel.text = [allFunctions[indexPath.row] getTitle];
and in didSelectRowAtIndexPath it'll call [_viewController runFunction:allFunctions[indexPath.row]];
How would I do that? I tried creating a method with an id *
parameter, which takes the class object, with the intent of storing that in the array allFunctions
I'm having trouble making a method that'll store all the functions because it's giving me the "implicit conversion of a non-objective-c pointer to autoreleasing id is disallowed with ARC" when I try to call [self registerClass:reset].
What should I do?
Upvotes: 2
Views: 702
Reputation: 90571
Class objects are objects, too. Rather than id
, you use a Class
variable to hold a reference to a class object.
Class theClass = [ResetClass class];
You can store these in an NSMutableArray
.
[someArray addObject:theClass];
You can later retrieve the class and send messages to it:
Class theClass = someArray[index];
id object = [[theClass alloc] init];
If you need to be able to query each class object for a title
property, you'll need to implement on each class that you add to your array. For example, this is a class method implementing such a class property:
+ (NSString*) title
{
return @"Blah, blah, blah";
}
You could obtain it using code like:
Class theClass = someArray[index];
NSString* title = [theClass title];
// ... code which uses the title ...
Upvotes: 3