【问题标题】:how to make tabbar based on my json response array (ios swift)如何根据我的 json 响应数组制作标签栏(ios swift)
【发布时间】:2016-01-19 04:51:15
【问题描述】:

问题:我想根据我的 JSON 响应数组创建标签栏,这意味着,如果我有 6 个元素作为响应,它将创建 6 个标签。

尝试过:我已经使用水平滚动集合视图制作了它,但我想通过原始标签栏制作它。

那么,我该怎么做呢?

please tell me the possible solutions and dont put this on hold..

这是我的回复,我该怎么办?

tabs =     (
            {

        id = 0;
        name = Home;

    },
            {

        id = 1;
        name = Winkel;

    },
            {

        id = 2;
        name = Zoeken;

    }

);
})

感谢@Ankit 的快速代码,但是当使用您的代码并传递名为“arr”的数组时出现此错误无法将“NSMutableArray”类型的值转换为预期的参数类型“[[String : Any]]”

这是我的代码

func web()
{

    request(.GET, "http://www.horecasupply.nl/AJAX?function=appStructure", parameters: nil, encoding: .JSON).responseJSON { (response:Response<AnyObject, NSError>) -> Void in
        print(response.result.value)

        if (response.result.value != nil)
        {
            self.arr = (response.result.value)!["tabs"] as! NSMutableArray

            print(self.arr)
        }


        loadTabbarsWithArray(arr)

    }

以上你可以显示我的 json 响应,我该如何解决它

【问题讨论】:

    标签: ios swift swift2 uitabbarcontroller ios9


    【解决方案1】:

    UITabbarController 中的标签数量取决于我们在标签栏中添加的 ViewControllers/NavigationControllers 的数量。

    根据服务响应的计数,您可以在运行时更改选项卡栏中的视图控制器数量。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/#//apple_ref/occ/instp/UITabBarController/viewControllers

    /*
    tabs =     (
                {
    
                    id = 0;
                    name = Home;
    
                },
                {
                    /...
                }
                )
    */
    
    - (void) loadTabbarsWithArray:(NSArray*)tabs{
    
        if (self.tabBarController == nil) {
            self.tabBarController = [[UITabBarController alloc] init];
        }
        self.tabBarController.viewControllers = [NSArray array];
    
        NSMutableArray *viewControllers = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *record in tabs) {
            UIViewController *viewController = [[UIViewController alloc] initWithNibName:"CustomViewController" bundle:nil];
            viewController.title = record[@"name"];
            viewController.tabBarItem.title = record[@"name"];
            [viewControllers addObject:viewController];
        }
        [self.tabBarController setViewControllers:viewControllers];
    
    }
    

    在斯威夫特中

    func loadTabbarsWithArray(let tabs:[[String: Any]]){
    
            if (self.tabBarController == nil) {
                self.tabBarController = UITabBarController();
            }
            tabBarController!.viewControllers = [UIViewController]();
    
            var viewControllers = [UIViewController]();
            for  record:[String: Any] in tabs {
                let viewController:UIViewController = UIViewController(nibName: "CustomViewController", bundle: nil);
                viewController.title = record["name"] as? String;
                viewController.tabBarItem.title = record["name"]as? String;
                viewControllers.append(viewController);
            }
            tabBarController!.viewControllers = viewControllers;
        }
    

    【讨论】:

    • 感谢您的建议我重写了我的问题你能告诉我我该怎么做吗?@Ankit
    • thanx @Ankit 在使用您的代码时如何调用此函数。因为当我像 loadTabbarsWithArray(arr) 一样调用它时,它向我显示错误无法将类型“NSMutableArray”的值转换为预期的参数类型“[[String : Any]]。
    • 这是我的数组内容 arr = ( { id = 0; name = Home; }, { id = 1; name = Winkel; }, { id = 2; name = Zoeken; } )
    猜你喜欢
    • 2020-05-08
    • 2022-11-12
    • 2017-05-26
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多