2011-11-16 66 views
0

我相信我可能正在處理一些視圖問題,這些問題不允許我檢測並將UIImageView對象添加到數組。我真的可以使用一些建議。將UIImage中的UIImageView對象添加到NSMutableArray

我已經得到了一些UIImageViews與鏈接到UIView上一個UIViewController頂部位於圖像(UIView加入幫助drawRect和一些額外的好處)。所以,當我觸摸圖像並「拖放」它時,我想在丟棄時將該圖像添加到數組中。

touchesBegan得到觸摸和檢查UIImageView被觸摸的位置,然後在該視圖中心如下:

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

      UITouch *touch = [[event allTouches] anyObject]; 

      CGPoint location = [touch locationInView:self]; //can't use self.view in a UIView, this may be causing issues? 

      startLocation = location; // for reference as needed 



    NSLog(@"Image x coord: %f", location.x); 

    NSLog(@"Image y coord: %f", location.y); 

    if ([touch view] == obiWan_Block) 

      {obiWan_Block.center = location;} 

    else if ([touch view] == r2d2_Block) 

      {r2d2_Block.center = location;} 

    else if ([touch view] == you_Block) 

      {you_Block.center = location;} 

} 

然後,我拖動UIImageView周圍touchesMoved最後,「降」的圖片與touchesEnded。當UIImageView放在屏幕的某個區域時,我會將其「捕捉」到特定位置。在這一點上,我想把這個UIImageView放入一個數組中,但我沒有運氣。我相信我對所觸及的各種觀點以及通過addObject添加到我的NSMutableArray的內容感到困惑。或者,我可能完全錯過了一些東西。這裏是我的touchedEnded方法:

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

      UITouch *touch = [[event allTouches] anyObject]; 

      CGPoint location = [touch locationInView:self]; 

      UIView *temp = [touch view]; 

      UIImageView *currentImageView = (UIImageView *)[touch view]; //DON'T THINK THIS IS RIGHT 

    NSLog(@"Current Image View is: %@", currentImageView); 

    if ((location.x > dropZone.origin.x) && (location.y >= dropZone.origin.y) && ([touch view] != NULL)) 

     { 

      CGRect frame = temp.frame; 

      if (touchCount == 0) { 

       frame.origin.x = 15; 

       frame.origin.y = 180; 

       temp.frame = frame; 

       [quoteArray addObject: currentImageView]; 

       touchCount++; 

       } 

      else if (touchCount == 1) { 

       frame.origin.x = 70; 

       frame.origin.y = 180; 

       temp.frame = frame; 

       [quoteArray addObject: currentImageView]; 

       touchCount++; 

       } 

       .... 

我有一個NSLog語句來檢查,如果addObject工作如下:

NSLog(@"the quote array contains %d items. Contents = %@",[quoteArray count], quoteArray); 

日誌總是說:

報價數組包含0項。內容=(空)

請指教,謝謝!

回答

1

您的代碼的最後一部分顯示您有未初始化的quoteArray。在創建它時檢查代碼,我猜你錯過了init方法中的某些東西。因爲如果陣列是正確的,那麼NSLog的應該顯示如下:

NSArray *quoteArray = [NSArray array]; 
NSLog(@"%@", quoteArray); 

2011-11-17 17:37:00.506 TestApp [1382:207]()

+0

謝謝!我確實在錯誤的地方初始化了我的'NSMutableArray'。總是需要數小時才能解決的簡單事情! :-) – jeffjohn01