Reputation: 4430
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
Upvotes: 1
Views: 4348
Reputation: 13860
Why your ViewControllers needs different names? You should simply create instance of one viewController class.
For example if you have a 3 "screens" from JSON:
And all of them have different buttons, text etc. And you have a class name MyViewController. This class may have a @property name, @property buttons (probably NSArray with object from your button management class) etc if you want to distinguish your screens.
Next you should store your objects (MyViewController class objects) in NSArray.
So what you want to do when app starts: You created an object of MyViewController class and display it. If user want to go to second screen you should simply create another instance of the same class.
So if you want to display name of the screen you have a self.name
value in your ViewController class.
Upvotes: 1
Reputation: 35616
Just create an array and hold them there. Something like this:
NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithCapacity:0];
// ...
// Inside a loop
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";
[viewControllers addObject:vc];
// Release vc and label if you're not using ARC
Now, if you want to name your controllers, one idea would be to create a subclass of UIViewController
and add a name
(or something like that) property. Then you just set this property also inside your loop and you can refer/filter based on that property.
Upvotes: 2
Reputation: 60110
You won't explicitly name them as separate variables, but rather you may have an NSArray of different UIViewController instances. As you read through your JSON file, you can loop over the information presented, creating a view controller and adding it to the array each time the JSON tells you to. When you're done, you can pull view controllers out of the array as your user navigates back and forth.
What does your JSON look like? Post an example and we may be able to provide more info.
Upvotes: 1