Swift:可以通过代码动态生成viewController吗?

问题描述

我通过API请求响应收到此消息:

"menus": {
    1 =     {
        id = 1;
        label = Accueil;
        maxnumberPerPage = 20;
    };
    2 =     {
        id = 2;
        label = "Mes favoris";
        maxnumberPerPage = 20;
    };
    3 =     {
        id = 3;
        label = "Les s\U00e9lections";
        maxnumberPerPage = 20;
    };
    4 =     {
        id = 4;
        label = "Acc\U00e8s illimit\U00e9";
        maxnumberPerPage = 20;
    };
},

根据此menu对象中元素的数量,我将生成一个不同的ViewController,其中将包含一个不同的UI和一个不同的逻辑。
这些ViewController的标题将是label对象的属性menu。 借助tabBar,用户将能够在这些viewController中进行导航。 所以这是到目前为止的结果:

enter image description here


这是创建TabBar的函数

   func createTabBarController() {
        let menus = CMSessionManager.sharedInstance.menus!
        var controllerArray : Array<UIViewController> = []
        for (key,menuInfo) in menus {
            if let menuInfoDetails = menuInfo as? [String: Any] {
                                        let newViewController = UIViewController()
                                        newViewController.title = menuInfoDetails["label"] as! String
                                        controllerArray.append(newViewController)
            }
        }
        tabBarCnt.viewControllers = controllerArray.map{ UINavigationController.init(rootViewController: $0)}
        self.view.addSubview(tabBarCnt.view)
    }

所以在这里,我在相同的tabBarController代码生成每个viewController:

  let newViewController = UIViewController()
  newViewController.title = menuInfoDetails["label"] 

我想要的是:
每个viewController都用一个单独的 label_ViewController.swift 文件表示。
这是我的问题:
我不知道menu对象中将有多少个项目。因此,我不知道应该预先创建多少 UIViewController 文件
无论如何我都看不出要解决这个问题。
问题是:
有什么办法解决这个问题?还是应该以编程方式进行所有操作?

注意::我需要将每个项目与 UIViewController 文件关联。这是因为viewController将以“物理方式”出现在情节提要中,因此每个文件将代表每个视图的逻辑。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)