2015-01-13 66 views
0

嘿大家,所以我有這樣的代碼,我想優化它的iOS的Xcode變量

,而不是到處都這樣做,我想將它添加在了,但是我不知道如何將它添加

_p1.userInteractionEnabled = YES; 
_p2.userInteractionEnabled = YES; 
_p3.userInteractionEnabled = YES; 
_p4.userInteractionEnabled = YES; 
_p5.userInteractionEnabled = YES; 
_p6.userInteractionEnabled = YES; 
_p7.userInteractionEnabled = YES; 
_p8.userInteractionEnabled = YES; 
_p9.userInteractionEnabled = YES; 

而不是此長碼我想將它添加一個用於將這個樣子

for (int i = 1; i <= 10; i++) 
    { 
     _p[i].userInteractionEnabled = YES; 
    } 
+2

也許'IBOutletCollection'是你想要的。它在很多地方都有描述,例如http://nshipster.com/ibaction-iboutlet-iboutletcollection/ – Mats

回答

1

至於建議的@Mats你應該使用IBOutletCollection,讓你有一個NSArray的意見。

然後,您可以也寫你的循環更加明顯,例如你的屬性稱爲interactiveViews並在您提供enabled作爲參數的方法,所以你可以重複使用它:

- (void)enableInteractiveViews:(BOOL)enabled 
{ 
    for (UIView *view in interactiveViews) { 
     view.userInteractionEnabled = enabled; 
    } 
}