2011-08-16 5 views
3

Objective-C에서 특정 규칙에 따라 인스턴스 이름을 얻는 방법은 무엇입니까?String을 사용하여 instanceName을 얻는 방법

타입 1 : 루프 GET 번호에 self.btn (0,1,2,3,4,5,6,7,8,9 ...)

을 해제 i 행 예

for(int i = 0; i<10; i++){ 
//wrong code (similar ActionScript3.0 code) 
self."btn"+i = [UIButton buttonWithType:UIButonTypeRoundRect]; 
//i think code but, not available 
self.[NSString stringWithFormat:@"btn%d", i] = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
} 

타입 2 : 그래서

for(int i =0; i<10; i++){ 
//wrong code (similar ActionScript3.0 code) 
UIButton *"btn"+i = [UIButton buttonWithType:UIButtonTypeRoundRect]; 
//i think code but, not available 
UIButton *[NSString stringWithFormat:@"btn%d", i] = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
} 

에서에서 루프에있는 UIButton의 *의 BTN (0,1,2,3,4,5,6,7,8,9 ...) 얻을 수 ActionScript3.0 소스가 아래에 있습니다.

for(var i=0; i<10; i++){ 
this["btn"+i] = new SimpeButton(); 
//btn0, btn1, btn2...btn10 get by for loop i object Simpebutton ten. 
} 

답변

1

대물 렌즈에는 버튼의 인스턴스 이름이 없습니다. 정수 값을 저장하여 적절한 버튼을 인식 할 수있는 태그 속성이 있습니다.

NSMutableArray *buttonsArray = [[NSMutableArray alloc] init]; 
for(var i=0; i<10; i++) { 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundRect]; 
    button.tag = i; 
    [buttonsArray addObject:button]; 
} 

를 다음 수있는 루프 하나 개의 방법이 배열 : 당신은 배열로 설정, 루프에 적절한 태그 속성과 저장소를 버튼을 만들 수 있습니다

for (UIButton *aButton in buttonsArray) { 
    // do something with aButton instance 
} 

또는 다른 :

for (int i=0; i<[buttonsArray count]; i++) { 
    // do something with [buttonsArray objectAtIndex:i]; 
} 
0

Objective-C에는 정확한 관용구가 없습니다. NSArray (또는 NSDictionary가 순차적으로 번호가 지정되지 않은 경우에는 NSDictionary)를 사용하면됩니다.

관련 문제