2010-11-24 112 views
0

是否有可能有一個可變的插座名稱? 例如,您有10個標籤(可能是公共汽車上的座位)。每個都有一個插座,seat1座2等。 是否有可能有一個for循環 連接@「座位」的增量整數。這樣我就可以訪問seat1,seat2出口而無需單獨指定它。 這不起作用,但使我想要實現的更清晰一些。變量IBOutlet名稱?

int i; 
for (i = 0; i < [seatarray count]; i++) 
{ 
    [@」seat」 stringByAppendingString[ i stringValue]] = @」」; 
} 

回答

0

可能更容易簡單地創建在加載時自己的數組:

self.seatarray = [NSArray arrayWithObjects:seat1, seat2, ..., seatN, nil]; 
6

開始的iOS4可以使用IBOutletCollection它允許多個實例連接到它代表對象的數組的單個出口,例如IBOutletCollection可存儲UILabels只:

@property (nonatomic, retain) IBOutletCollection(UILabel) NSArray *seats; 
+1

要知道,不過,即使IBOutletCollection需要一個NSArray,內容的順序是未指定的,並不總是它們在IB中連接的順序。這很不幸,因爲這將是一個使用它們的好地方。 – 2010-11-24 16:33:24

0

你應該能夠做到這一點與鍵 - 值編碼,像(未經測試的代碼)
for (int i = 0; i != 10; ++i) { [self setValue:@"foo" forKey:[@"seat" stringByAppendingFormat:@"%d", i]]; }