Search code examples
iosobjective-cuiviewshow-hide

VBPieChart - removeFromSuperView not working (hide chart)


I'm using VBPieChart, in Objective C language. I'm showing the chart on valueChange of a segmented control, like :

- (IBAction)segmentControlAction:(UISegmentedControl *)sender
    {
    switch ([sender selectedSegmentIndex])
    {
        case 0:

            [self showPieChart:NO];
            _tableViewForCount.hidden = NO;
            [_tableViewForCount reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationRight];

            break;
        case 1:

            _tableViewForCount.hidden = YES;

            [self showPieChart:YES];

            break;
        default:
            NSLog(@"None");
            break;
    }
}

and the showPieChart method goes like :

- (void) showPieChart:(Boolean)visible
{
    VBPieChart *chart = [[VBPieChart alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
    chart.center = self.view.center;

    // Setup some options:
    [chart setHoleRadiusPrecent:0.3]; /* hole inside of chart */

    // Prepare your data
    NSMutableArray *chartValues = [[NSMutableArray alloc]initWithObjects:                             @{@"name":@"Apples", @"value":@50, @"color":[UIColor redColor]},
                             @{@"name":@"Pears", @"value":@20, @"color":[UIColor blueColor]},
                             @{@"name":@"Oranges", @"value":@40, @"color":[UIColor orangeColor]},
                             @{@"name":@"Bananas", @"value":@70, @"color":[UIColor purpleColor]}, nil
                             ];


    if (visible)
    {
        chart.center = self.view.center;
        // Present pie chart with animation
        [chart setChartValues:chartValues animation:YES duration:0.4 options:VBPieChartAnimationFan];
        [self.view addSubview:chart];
    }
    else
    {
        // remove chart
        [chart removeFromSuperView];
    }
}

Since VBPieChart is just a custom subclass of UIView, this should work. But i'm not getting any success.

I've also tried [chart setHidden:YES]; as well as [chart setAlpha:0.0]; , but still no luck.

Any help would is appreciated. I just want to hide/remove the chart when user switched back to segment index 0.

Thanks.


Solution

  • VBPieChart *chart create this object on the .h file then

    - (void) showPieChart:(Boolean)visible
    {
        if(chart == nil)
        {
            chart = [[VBPieChart alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
            [self.view addSubview:chart];
        }
        chart.center = self.view.center;
    
        // Setup some options:
        [chart setHoleRadiusPrecent:0.3]; /* hole inside of chart */
    
        // Prepare your data
        NSMutableArray *chartValues = [[NSMutableArray alloc]initWithObjects:                             @{@"name":@"Apples", @"value":@50, @"color":[UIColor redColor]},
                                 @{@"name":@"Pears", @"value":@20, @"color":[UIColor blueColor]},
                                 @{@"name":@"Oranges", @"value":@40, @"color":[UIColor orangeColor]},
                                 @{@"name":@"Bananas", @"value":@70, @"color":[UIColor purpleColor]}, nil
                                 ];
    
    
        if (visible)
        {
            chart.center = self.view.center;
            // Present pie chart with animation
            [chart setChartValues:chartValues animation:YES duration:0.4 options:VBPieChartAnimationFan];
            chart.alpa = 1.0
        }
        else
        {
            // remove chart
            chart.alpa = 0.0
        }
    }