Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
378 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...