0

我的问题已经2天了,仍然无法解决。我实际上在做的是尝试刷新 Graph Hosting View。当我使用 Graph Hosting View 打开 UIViewController 时,会自动绘制一个标准图。我想要的是根据用户可以通过文本字段输入的一些系数来更改图形。

- (void)viewWillAppear:(BOOL)animated
{   
[super viewWillAppear:animated];
float x = -10;
float a = [coeff1.text floatValue];
 NSMutableArray *data = [NSMutableArray array];
for (x; x <= 10; x=x+0.2)
{
    [data addObject:[NSValue valueWithCGPoint:CGPointMake(x , a*x+1)]];
}
self.scatterPlot = [[TUTSimpleScatterPlot alloc] initWithHostingView:_graphHostingView     andData:data];
[self.scatterPlot initialisePlot];

您可能会看到,我想要的只是当用户为“coeff1.text”输入新值时要刷新的视图,请原谅这个问题可能很愚蠢,但老实说,我没有看到任何解决方法,我已经尝试过很多事情,我仍然无法刷新图表。我认为以某种方式可能是通过输入新系数并单击按钮(IBAction)来更新图表......

提前谢谢你,埃里克

4

1 回答 1

0

首先,您错误地解析了一个浮点值。这就是你的做法:

  NSNumberFormatter *formatter = [NSNumberFormatter new];
  [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
  NSNumber * myNumber = [formatter numberFromString:coeff1.text];

接下来假设它coeff1实际上是一个 UITextField,您应该能够像这样注册它的编辑更改更新

- (void)viewDidLoad {
  [super viewDidLoad];
  [coeff1 addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
}

- (void)editingChanged:(UITextField *)textField {
  NSNumberFormatter *formatter = [NSNumberFormatter new];
  [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
  NSNumber * myNumber = [formatter numberFromString:coeff1.text];
  NSMutableArray *data = [NSMutableArray array];
  for (x = 0; x <= 10; x=x+0.2)
  {
    [data addObject:[NSValue valueWithCGPoint:CGPointMake(x , myNumber.floatValue*x+1)]];
  }
  self.scatterPlot = [[TUTSimpleScatterPlot alloc] initWithHostingView:_graphHostingView     andData:data];
  [self.scatterPlot initialisePlot];

}
于 2013-03-27T01:35:04.377 回答