I'm trying to create an app that pulls json from an api with a set of instructions on how the interface must look like. So basically the json will contain an array of NSDictionary and each NSDictionary will be an object that is displayed on the screen.
In the NSDictionary will be all the details of how the object will be displayed such as type of object, location of object and size of the object.
I have written code to accept an array of buttons to the screen.
for (int i = 0; i < self.jsonObjects.count; i++) {
NSDictionary *jsonObject = [self.jsonObjects objectAtIndex:i];
if ([[jsonObject objectForKey:@"object"] isEqualToString:@"UIButton"]) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
NSNumber *x = [jsonObject objectForKey:@"xlocation"];
NSNumber *y = [jsonObject objectForKey:@"ylocation"];
NSNumber *width = [jsonObject objectForKey:@"width"];
NSNumber *height = [jsonObject objectForKey:@"height"];
NSString *title = [jsonObject objectForKey:@"title"];
[button setFrame:CGRectMake(x.intValue, y.intValue, width.intValue, height.intValue)];
[button setTitle:title forState:UIControlStateNormal];
[self.view addSubview:button];
}
}
Now I can have tons of if statements for each object and get the program to do the same thing but I am trying to avoid it.
Basically what I am asking is what is the best way to implement this to minimise coding and increase readability of the code.
This is code I wrote to mimic the json output for the button.
NSDictionary *button = [[NSDictionary alloc] initWithObjectsAndKeys:@"UIButton", @"object",@"PressMe",@"title",@"10",@"xlocation",@"10",@"ylocation",@"100",@"width",@"100",@"height", nil];
NSDictionary *button2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"UIButton", @"object",@"Dont Press",@"title",@"10",@"xlocation",@"210",@"ylocation",@"100",@"width",@"100",@"height", nil];
self.jsonObjects = [[NSArray alloc] initWithObjects:button,button2, nil];
Since I must still create the api the json output can be very flexible.
I was thinking of having an array of arrays. where each array in the array is an array of buttons or array of textfields. then I will only need about 20-30 arrays to cover the different object types and can run through the main array and then run through the array of each button or textfield.
for Eli Ganem
object UIView * 0x07145c60
UIResponder UIResponder
_layer CALayer * 0x07145e80
_tapInfo id 0x00000000
_gestureInfo id 0x00000000
_gestureRecognizers NSMutableArray * 0x00000000
_subviewCache NSArray * 0x075213e0
_charge float 0
_tag NSInteger 0
_viewDelegate UIViewController * 0x00000000
_backgroundColorSystemColorName NSString * 0x00000000
_viewFlags <anonymous struct>