xCode 动态创建 ViewController [英] xCode Dynamically create ViewControllers

查看:12
本文介绍了xCode 动态创建 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够基于 JSON 文件动态创建 ViewController.我的意思是,会有一个 json 来指示用户需要多少 ViewController.也就是说,我有一个 json 文件,其中列出了 5 个 ViewController,我希望能够动态创建这些 ViewController 并能够在它们之间进行转换.

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.

所以我将拥有的是 JSON 文件,它列出了 ViewControllers,在这个例子中说 3.此 JSON 文件包含有关文本、按钮等以及如何在它们之间导航的信息.

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.

所以我希望能够遍历这个 JSON,并创建必要的视图控制器并添加所需的文本、按钮等.JSON 还将指示如何视图控制器链接在一起.

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.

我知道如何创建一个 VC 并添加这样的信息(这只是一个简单的例子,刚刚创建了 vc 并添加了标签.

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];

我不知道如何使用 JSON 在循环中创建多个不同名称的 ViewController.有人对如何做到这一点有任何想法吗?或者这样的事情甚至可能吗?

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?

任何帮助将不胜感激.

JSON 的基本示例

{
   "ViewControllers":[
      {
         "name":"FirstVC",
         "id":1
      },
      {
         "name":"SecondVC",
         "id":2
      },
      {
         "name":"ThirdVC",
         "id":3
      }
   ]
}

所以第一个 VC 链接到 secondVC 和第二个到第三个VC

So first VC links to secondVC and second to thirdVC

推荐答案

只需创建一个数组并将它们保存在那里即可.像这样:

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

现在,如果你想给你的控制器命名,一个想法是创建一个 UIViewController 的子类并添加一个 name(或类似的东西)属性.然后您只需在循环中设置此属性,您就可以根据该属性引用/过滤.

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.

这篇关于xCode 动态创建 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆