I want to be able to dynamically create ViewControllers based on a JSON file. What I mean is, there will be a json that will dictate how many ViewControllers the user needs. I.e say I have a json file that lays out 5 ViewControllers, I want to be able to dynamically create these ViewControllers and be able to transition between them.
So what I am going to have is JSON file, that sets out the ViewControllers, say 3 for this example. This JSON file has info on the text, buttons etc and how to navigate between them.
So I want to be able to loop through this JSON, and create the necessary view controllers and add the required text, buttons etc. The JSON will also dictate how the view controllers link up together.
I know how to create one VC and add info like this (This is just quick example, just created vc and added label.
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor whiteColor];
UILabel *testLabel =[[UILabel alloc] initWithFrame:CGRectMake(220, 50, 130, 80)];
testLabel.backgroundColor = [UIColor clearColor];
testLabel.textColor = [UIColor blackColor];
testLabel.text = @"Hello";;
[vc.view addSubview:testLabel ];
[self.navigationController pushViewController:vc animated:YES];
I don’t know how to create several differently named ViewControllers in a loop using JSON. Anyone have any ideas on how to do this? Or is something like this even possible?
Any assistance would be greatly appreciated.
EDIT:
Very basic example of what JSON will look like
{
"ViewControllers":[
{
"name":"FirstVC",
"id":1
},
{
"name":"SecondVC",
"id":2
},
{
"name":"ThirdVC",
"id":3
}
]
}
So first VC links to secondVC and second to thirdVC
Just create an array and hold them there. Something like this:
Now, if you want to name your controllers, one idea would be to create a subclass of
UIViewControllerand add aname(or something like that) property. Then you just set this property also inside your loop and you can refer/filter based on that property.