2011-05-25 53 views
2

嗨的NSArray的, 所以....可以說我有圖片如何使用一個for循環來顯示的UIImage的

NSMutableArray *images = [NSMutableArray new]; 
[images addObject:[UIImage imageNamed:@"line1.png"]]; 
[images addObject:[UIImage imageNamed:@"line2.png"]]; 
[images addObject:[UIImage imageNamed:@"line3.png"]]; 
[images addObject:[UIImage imageNamed:@"line4.png"]]; 

現在的一個NSArray我想加載所有這些都是使用一次一個for循環,但這裏是捕捉....我需要能夠將圖像設置爲隱藏,直到用戶通過交互取消隱藏。

for (UIImage *image in images) { 
    UIImageView *line = [[UIImageView alloc] initWithImage:image]; 
    line.hidden = YES; 
    [self.view addSubview:line]; 
} 

但是,然後如何使用另一種方法將隱藏的BOOL設置爲NO?

作爲第二個問題,如何在上面的代碼中釋放*行?

感謝, 達倫

回答

5

一種選擇是設置你的相似圖片:

int nextTag = 1; 
for (UIImage *image in images) { 
    UIImageView *line = [[UIImageView alloc] initWithImage:image]; 
    line.hidden = YES; 
    line.tag = nextTag; 
    [self.view addSubview:line]; 
    [line release]; 
    nextTag++; 
} 

...然後取消隱藏他們,你可以這樣做:

UIView* imageView = [self.view viewWithTag: lineNumber]; 
imageView.hidden = NO; 

.. 。假設你的用戶交互處理程序能夠確定用戶正在與用戶交互的UI中的哪一行。

+0

謝謝aroth,這可能是缺少的環節,我需要。我沒有想到使用標籤。但是如何釋放所有圖像的問題依然存在。有關於此的任何想法? – EcksMedia 2011-05-25 23:52:58

+0

好吧,我已經做了一些測試,因爲我第一次考慮不可能設置隱藏選項卡,因爲沒有對每個ImageView的引用。我錯過了什麼,或者我應該重新考慮這種情況? – EcksMedia 2011-05-26 01:29:15

+0

@EcksMedia - 將您的UIImageView作爲子視圖添加後即可發佈。超級景觀將自動保留它,直到你完成它。你應該能夠通過調用'viewWithTag:'來檢索一個imageView引用,只要你能夠找出正確的標籤是什麼。根據用戶的輸入,這取決於您的用戶界面。基本上你需要用某種方式說:「用戶觸摸位置'[x]'的控件,這意味着我需要用標籤'[y]'」取消隱藏imageView。 – aroth 2011-05-26 02:22:02

2

作爲第二個問題,如何在上面的代碼中釋放*行?

for (UIImage *image in images) { 
    UIImageView *line = [[UIImageView alloc] initWithImage:image]; 
    line.hidden = YES; 
    [self.view addSubview:line]; // this retains the subview. 
    [line release];    // release line like this. 
} 
+0

夠公平的,這是有道理的。謝謝。 – EcksMedia 2011-05-26 01:24:16

0
** 

-(IBAction)btnReviewStar:(id)sender{ 
    for (int i =([sender tag] ==30); i<36; i++) { 
     btnReviewStar.selected = NO; 
     btnReviewStar1.selected = NO; 
     btnReviewStar2.selected = NO; 
     btnReviewStar3.selected = NO; 
     btnReviewStar4.selected = NO; 
     if([sender tag] == 31) { 
      btnReviewStar.selected = YES; 
      break; 
     } else if([sender tag]==32) { 
      btnReviewStar.selected = YES; 
      btnReviewStar1.selected = YES; 
      break; 
     } else if([sender tag]==33) { 
      btnReviewStar.selected = YES; 
      btnReviewStar1.selected = YES; 
      btnReviewStar2.selected = YES; 
      break; 
     } else if([sender tag]==34) { 
      btnReviewStar.selected = YES; 
      btnReviewStar1.selected = YES; 
      btnReviewStar2.selected = YES; 
      btnReviewStar3.selected = YES; 
      break; 
     } else { 
      btnReviewStar.selected = YES; 
      btnReviewStar1.selected = YES; 
      btnReviewStar2.selected = YES; 
      btnReviewStar3.selected = YES; 
      btnReviewStar4.selected = YES; 
      break; 
     } 
    } 
} 

** 
相關問題