2011-10-24 8 views
0

我有很多UIView對象;從整數更改UIViewObject名稱

//View 2.Satir 
UIView *view11; 
UIView *view12; 
UIView *view13; 
UIView *view14; 
UIView *view15; 
UIView *view16; 
UIView *view17; 
UIView *view18; 
UIView *view19; 
UIView *view20; 

,我想從他們的循環在這個例子中閱讀,如:

int i; 
for (i=0; i<1; i++) { 
    if ([[ratio objectAtIndex:i]intValue] == 100) { 
     rd = 0; 
     gr = 0.5; 
     bl =0; 

     view1.backgroundColor = [UIColor colorWithRed:rd green:gr blue:bl alpha:1.0]; 

     /* i want to do this 
     view(i).backgroundColor = .......... 
     i wanna return all views in for loop */ 
    } 
} 

我會怎麼做呢?

+0

把所有的意見如果它們是在界面構建器中設置的,則返回一個數組或IBOutletCollection。 – jrturton

回答

0

把所有視圖中的數組

然後循環通過該陣列,並要求該數組的第i個指數。 然後,您將獲得所需的適當UIView。

1

插入的意見,一個NSMutableArray的對象:

NSMutableArray *viewArray = [NSMutableArray array]; 
    [viewArray addObject:viewXX]; 

然後通過遍歷:

int index = 0; 
    for (UIView *view in viewArray) { 
     if ([[ratio objectAtIndex:index] intValue] == 100) { 
      rd = 0; 
      gr = 0.5; 
      bl = 0; 

      UIView *tempView = [viewArray objectAtIndex:i]; 
      tempView.backgroundColor = [UIColor colorWithRed:rd green:gr blue:bl alpha:1.0]; 
     } 

     index++; 
    } 
+0

感謝很多everbody ..它的工作:) – coexploit

+0

建議使用'for(NSView * view in viewArray)'...語法來代替。它通常更快,因爲它避免了每次迭代發送幾條消息。 – jer

-1

插入所有的意見,一個NSMutableArray 像:

NSMutableArray *viewArray = [NSMutableArray array]; 
[viewArray addObject:myView]; 

    int index = 0; 
    while (index<[viewArray count]) { 
    if ([[ratio objectAtIndex:index] intValue] == 100) { 
     rd = 0; 
     gr = 0.5; 
     bl =0; 

     UIView *tempView = [viewArray objectAtIndex:i]; 
     tempView.backgroundColor = [UIColor colorWithRed:rd green:gr blue:bl alpha:1.0]; 
     index++; 
} 
+0

謝謝你它工作:) – coexploit