【问题标题】:VBPieChart - removeFromSuperView not working (hide chart)VBPieChart - removeFromSuperView 不起作用(隐藏图表)
【发布时间】:2017-04-21 06:19:35
【问题描述】:

我使用的是 VBPieChart,在 Objective C 语言中。我在分段控件的valueChange 上显示图表,例如:

- (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 时隐藏/删除图表。

谢谢。

【问题讨论】:

    标签: ios objective-c uiview show-hide


    【解决方案1】:

    VBPieChart *chart 在.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
        }
    }
    

    【讨论】:

      【解决方案2】:

      您需要创建如下变量。

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

      【讨论】:

        猜你喜欢
        • 2018-01-21
        • 2012-08-13
        • 1970-01-01
        • 1970-01-01
        • 2016-02-02
        • 1970-01-01
        • 1970-01-01
        • 2013-04-30
        • 2018-02-07
        相关资源
        最近更新 更多