2012-02-08 35 views
0

我想存儲我的touchesMoved值到NSArray我怎麼能存儲。現在我嘗試存儲我的觸摸移動值,但我不會打印價值。 這裏是我的代碼存儲touchesMoved價值NSArray在iPhone

- (void)initWithSize:(int)nX height:(int)nY { 
    xWidth = nX; 
    yHeight = nY; 

     xArray = [[NSMutableArray alloc] initWithCapacity:xWidth]; 

     for (int i = 0; i < xWidth; i++) { 

      yArray = [[NSMutableArray alloc] initWithCapacity:yHeight]; 

      for (int j = 0; j < yHeight; j++) {     

      } 

      roomArray = [[NSArray alloc] initWithArray:yArray]; 
      [xArray addObject:roomArray]; 
      [roomArray release]; 
      [yArray release]; 


     } 

我把這種方法是這樣

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self]; 

    [self initWithSize:currentPoint.x height:currentPoint.y]; 

} 

我打印這樣

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

     NSString *myArrayString = [xArray description]; 
      NSLog(@"screeX:%@ ",xArray);  
    } 

這個值,但餘did't得到,在我的控制檯輸出像素 我得到這樣的輸出

screeX:(
     (
    ), 
     (
    ), 
     (
    ), 
     (
    ), 
     (
    ), 
     (
    ), 
     (
    ), 
     (
+0

根據你的代碼,數組是分配每次whwn觸摸移動... – vishy 2012-02-08 06:23:25

+0

@vishy告訴我我該怎麼做才能將該值存儲到數組中? – 2012-02-08 06:26:21

+1

嘗試下面的答案,並讓我知道結果 – vishy 2012-02-08 06:39:48

回答

1

你實施的方式並不合適。

  1. 在.h文件中聲明NSMutableArray對象。

    NSMutableArray *_array; 
    
  2. 您可以在viewDidLoad中分配數組。

    _array = [[NSMutableArray alloc] init]; 
    
  3. touchesBegin,取出數組的所有對象。

    [_array removeAllObjects]; 
    
  4. touchesMoved,如下添加CGPoint s到該陣列。

    [_array addObject:[NSValue valueWithCGPoint: currentPoint]]; 
    

現在,在touchesEnded,打印陣列和看到的值。

+0

謝謝你的回答我做了所有的步驟,但我走出這樣screeX:(null) – 2012-02-08 06:43:51

+0

我已經更新了我的答案..你確定你已經完成了像這個? – Ilanchezhian 2012-02-08 07:00:00

+0

另外,請確保你的'viewDidLoad'正在被調用。否則,在類的'init'方法中分配數組。 – Ilanchezhian 2012-02-08 07:01:31