2017-04-21 35 views
0

我在Objective C語言中使用VBPieChart。我顯示在圖表上的分段控制的valueChange,如:VBPieChart - removeFromSuperView不工作(隱藏圖表)

- (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; 
    } 
} 

showPieChart方法是這樣:

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

由於VBPieChart只是的UIView的定製子類,這應該工作。但我沒有取得任何成功。

我也試過[chart setHidden:YES];以及[chart setAlpha:0.0];,但仍然沒有運氣。

任何幫助將不勝感激。我只是想在用戶切換回段索引0時隱藏/刪除圖表。

謝謝。

回答

1

VBPieChart *圖表.h文件中創建該對象,然後

- (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 
    } 
} 
+0

謝謝。奇蹟般有效。 –

+0

@RajD最受歡迎編碼! –

0

您需要如下創建變量。

static VBPieChart *chart = nil 
if(!chart) 
    chart = [[VBPieChart alloc] initWithFrame:CGRectMake(0, 0, 250, 250)]; 
    } 
    if (visible) 
    { 
     chart.hidden = NO; 
    } 
    else 
    { 
     // hide chart 
     chart.hidden = YES; 
    }