2011-01-11 54 views
0

我一直在嘗試調整加速度計示例中的代碼,以便當用戶壓下一個uibutton時,一個點被添加到折線圖中。來自NSMutableArray的折線圖CGPoints

工作的轉換兩個浮體,其計算如下成CGPoint的結果,並且將所述CGPoint成NSValue,然後用以下

-(IBAction)calculate:(id)sender { 
    self.points = [NSMutableArray array]; 
    CGPoint pt = CGPointMake(d, c); 
    [self.points addObject:[NSValue valueWithCGPoint:pt]]; 
    NSLog(@"%@", NSStringFromCGPoint(pt)); 
    NSLog(@"%@", [NSString stringWithFormat:@"%d points", self.points.count ]); 
} 

但由於某些原因添加這一個的NSMutableArray我只收到一個存儲在數組中的對象,似乎每次按下計算按鈕時,對象pt都會被覆蓋,而在正面它具有正確的x,y座標。任何想法在這一個?

UPDATE

Removed self.points = [NSMutableArray array];並將其放置在視圖中,並將其設置爲0,0。所以這是行得通的。現在接下來的問題是我有一個Graph子類,CG繪圖正在發生。我試圖找出一個簡單的方法來訪問上面的圖形類的ViewController類中的NSMutableArray。

我很接近結束,但我真的陷入困境,任何幫助都會很大。 仍嘗試在UIScrollview上的UIView上繪製線圖。繪製矩形方法在UIView子類中,一切都在那裏工作,我在軸上有網格線和標籤,我可以手動繪製它。但是我遇到的問題是我無法讀取CGPoints的NSMutableArray,它們是x和y座標。 ViewController執行計算並將結果寫入NSMutable數組,並且這一切都工作正常,我可以看到CGPoint和它們的值正在使用ViewController中的NSLog編寫。 我已經嘗試過各種方法將NSMutableArray設置爲全局但無效,一切運行,但雖然我可以看到在ViewController中寫入的點,但它們對UIView子類不可見。 我也嘗試過使用addObserver和observeValueForKeyPath方法,並且一旦所有運行的子類都看不到數組。 任何想法,建議,提示或想法將是偉大的

+0

移除self.points = [NSMutableArray的數組]相關男女混合;並將其放置在視圖中,並將其設置爲0,0。所有的工作 – Mattog1456 2011-01-12 03:05:44

回答

0

得到了所有工作,並學到了很多,一路走來,只是出於興趣最終產品的大致輪廓是

  • 視圖控制器具有滾動型和 的UIView的圖形
  • UIView子類處理圖
  • Singleton類爲NSMutable 數組,因此ViewController可以將值寫入 ,並且UIView子類 可以讀取值t o畫出 圖。

這是來自UIView子類

- (void)drawRect:(CGRect)rect { 

    float what = self->m_Val1; 
    NSLog(@"what %f",what); 

    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGColorRef black = [[UIColor blackColor] CGColor]; 

    // Draw the grid lines 
    DrawGridlines(ctx, 0.0, 200); 


    // Draw the text 
    UIFont *systemFont = [UIFont systemFontOfSize:12.0]; 
    [[UIColor whiteColor] set]; 
    [@"20" drawInRect:CGRectMake(2.0, 1.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft]; 
    [@"15" drawInRect:CGRectMake(2.0, 50.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft]; 
    [@"10" drawInRect:CGRectMake(2.0, 100.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft]; 
    [@"5" drawInRect:CGRectMake(2.0, 150.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft]; 


    if (what < 1) return; 

    CGContextSetStrokeColorWithColor(ctx, black); 
    CGContextSetLineWidth(ctx, 4.0f); 
    MySingleton  *resultsInstance = [MySingleton instance]; 
    NSLog(@"From Graph view Count: %d", [[resultsInstance getArray] count]); 
    float counthis =([[resultsInstance getArray] count]); 
    for (int i = 0; i < (counthis-1); i++) 
    { 
     CGPoint pt1 = [[[resultsInstance getArray] objectAtIndex:i]CGPointValue]; 
     CGPoint pt2 = [[[resultsInstance getArray] objectAtIndex:i+1]CGPointValue]; 
     CGContextMoveToPoint(ctx, pt1.x, pt1.y); 
     CGContextAddLineToPoint(ctx, pt2.x, pt2.y); 
     CGContextStrokePath(ctx); 
     NSLog(@"cgpoint %@", NSStringFromCGPoint(pt1)); 
     NSLog(@"point x,y: %f,%f", pt1.x, pt1.y); 

    } 

} 
0

這聽起來像你所有的trackcalc []值爲零。這又反過來表明[vallres.text floatValue]返回0.

[NSString floatValue]當它不能返回數字時返回零。

的floatValue

返回接收的 文字作爲浮動的浮點值。

- (浮動)的floatValue

返回值

接收機的 文字作爲浮動的浮點值,在字符串的開頭跳過空白 。 返回HUGE_VAL或-HUGE_VAL, 溢出,0.0下溢。並且 返回0.0,如果接收者不是 以浮點數的有效文本表示 開頭。

+0

感謝westsider,我用NSMutableArray方法取得了一些進展。 – Mattog1456 2011-01-12 05:44:51