根据json值,我们如何将视图控制器从一个视图移动到另一个视图?

时间:2015-09-01 09:30:42

标签: ios objective-c

JSON OUTPUT:

 category =     (
                {
            "category_id" = 1;
            column = 0;
            description = "";
            image = "images/Car Wash.jpg";
            "meta_description" = "";
            "meta_keyword" = "";
            name = "Car Wash";
            "parent_id" = "";
            "sort_order" = 0;
            status = enable;
            subcategory =             (
                                {
                    "category_id" = 6;
                    column = 0;
                    description = "";
                    image = "images/hatchpack.jpg";
                    "meta_description" = "";
                    "meta_keyword" = "";
                    name = hatchpack;
                    "parent_id" = 1;
                    "sort_order" = 0;
                    status = enable;
                    subcategory =                     (
                    );
                    top = "";
                }
            );
            top = "";
        },
                {
            "category_id" = 2;
            column = 0;
            description = "";
            image = "images/Pest Control.png";
            "meta_description" = "";
            "meta_keyword" = "";
            name = "Pest Control";
            "parent_id" = "";
            "sort_order" = 0;
            status = enable;
            subcategory =             (
            );
            top = "";
        },
                {
            "category_id" = 3;
            column = 0;
            description = "";
            image = "images/Electrician.jpg";
            "meta_description" = "";
            "meta_keyword" = "";
            name = Electrician;
            "parent_id" = "";
            "sort_order" = 0;
            status = "";
            subcategory =             (
            );
            top = "";
        },
                {
            "category_id" = 4;
            column = 0;
            description = "";
            image = "images/Washing Machine.jpg";
            "meta_description" = "";
            "meta_keyword" = "";
            name = "Washing Machine";
            "parent_id" = "";
            "sort_order" = 0;
            status = "";
            subcategory =             (
            );
            top = "";
        },
                {
            "category_id" = 5;
            column = 0;
            description = "";
            image = "images/TV Repair.png";
            "meta_description" = "";
            "meta_keyword" = "";
            name = "TV Repair";
            "parent_id" = "";
            "sort_order" = 0;
            status = enable;
            subcategory =             (
            );
            top = "";
        },
                {
            "category_id" = 6;
            column = 0;
            description = "";
            image = "images/hatchpack.jpg";
            "meta_description" = "";
            "meta_keyword" = "";
            name = hatchpack;
            "parent_id" = 1;
            "sort_order" = 0;
            status = enable;
            subcategory =             (
            );
            top = "";
        }
    );
    success = 1;
}

我的代码:

 for(NSDictionary *DicHoleCategories in ArrCategory)
        {
            [DicAllValues setObject:[DicHoleCategories objectForKey:@"subcategory"] forKey:@"subcategory"];
            [ArrName addObject:DicAllValues];
        }
           //did select row at indexpath method//
            subCategory = [[ArrName objectAtIndex:indexPath.row] objectForKey:@"subcategory"];

    if(subCategory==nil||[subCategory isEqual:@""])
        {
            productScreen *screen = [[productScreen alloc]init];
            screen  = [self.storyboard instantiateViewControllerWithIdentifier:@"product"];
            [self presentViewController:screen animated:YES completion:nil];
        }
        else
        {

            CarDetailsVC *car = [[CarDetailsVC alloc]init];
            car=[self.storyboard instantiateViewControllerWithIdentifier:@"category"];
            [self presentViewController:car animated:YES completion:nil];
        }

这里我已经正确地提到了我的代码,已经完成了子类别值并且我已经比较了值为null或字符串是空的,但是只有部分正在工作我必须做什么请任何人帮助我。

2 个答案:

答案 0 :(得分:1)

for(NSDictionary *DicHoleCategories in ArrCategory)
    {
        [DicAllValues setObject:[DicHoleCategories objectForKey:@"subcategory"] forKey:@"subcategory"];
        [ArrName addObject:DicAllValues];
    }
//did select row at indexpath method//
      NSArray  *subCategory=[[ArrName objectAtIndex:indexPath.row] objectForKey:@"subcategory"];
if(subCategory.count ==0)//subcategory is an array
    {
        productScreen *screen=[[productScreen alloc]init];
        screen =[self.storyboard instantiateViewControllerWithIdentifier:@"product"];
        [self presentViewController:screen animated:YES completion:nil];
    }
    else
    {

        CarDetailsVC *car=[[CarDetailsVC alloc]init];
        car=[self.storyboard instantiateViewControllerWithIdentifier:@"category"];
        [self presentViewController:car animated:YES completion:nil];
    }

答案 1 :(得分:0)

你给了&& if语句中的条件。这两个条件不能同时成立。此外,使用isEqualToString方法检查字符串。 所以把你的if条件放在下面 -

if([subcategory length] ==0){
   // Your code goes here.
}