Reputation: 381
I used to work on macro scripts for windows and this was doable:
VAR=0
Label>LOOP
VAR=VAR+1
ANOTHERVAR%VAR%=8 (all variables up to 30 will be set to 8)
if>VAR>=30 {Goto>BREAK}else{Goto>LOOP}
Label>BREAK
EXIT
The code above would effectively set "ANOTHERVAR1" up to "ANOTHERVAR30" to the value 8.
Now my question is, how can I achieve the same results on objective-c? I've got many variables to set. Right now on XCode i need to set 30 variables. I've always set them all one by one but it's taking up space and my source code is over 10k lines now.
This is what i've got :
if (Dedicatedkind==@"Cat"){
_oAttack7.alpha=0;
_oAttack7.enabled=NO;
}
This basically disables non-required buttons, in this case Dedicatedkind always equals Cat. I would need to disable buttons 7 to 30. But I've got over 80 different Dedicatedkind. I hope you understand my problem. I want to be able to disable any number of buttons with less lines of codes than it takes if I was to set them all manually.
Like this :
if (Dedicatedkind==@"Cat"){
_oAttack7.alpha=0;
_oAttack7.enabled=NO;
_oAttack8.alpha=0;
_oAttack8.enabled=NO;
_oAttack9.alpha=0;
_oAttack9.enabled=NO;
And so on, until I reach _oAttack30
}
If it matters at all, this is for the iphone and i'm working with storyboard.
Extra info:
Setting enabled:NO is simply a safety because if condition is met, those buttons turns alpha:0.3 which re-enables them. Unfortunately my code is too large to post every roots and conditions. Those buttons are attacks learned by the cat, there's 30 attacks maximum but the cat only learns 6 of them. Those 24 buttons must be completely invisible while the 6 attacks, even if not learned yet, must be at least partially visible. When learning those attacks, the appropriate buttons turns completely visible. That's for the cat. In this game, the cat is one of the three basic animals you get to have when starting a new game, therefore has limitations. Bigger, rarer animals may learn more than 6 attacks.
Fortunately, I've already done all of that except the 24 attacks/Buttons that must be disabled, which led me to this question!
Upvotes: 1
Views: 1212
Reputation: 726559
First, put your buttons into an IBOutletCollection(UIButton)
. You need to declare a property and make connections in the interface builder.
Next, create an NSDictionary
that maps Dedicatedkind
to instances of NSIndexSet
. Initialize the dictionary with 80 mappings - one for each kind. With this dictionary and the button array in hand, you would be able to look up an index set by Dedicatedkind
, iterate through indexes contained in it, and enable/disable buttons as required in a simple loop. As an added bonus, your long chain of if
/then
/else
will be gone too.
Header:
@property (nonatomic, readwrite) IBOutletCollection(UIButton) NSArray *allButtons;
NSDictionary *buttonIndexByKind;
Designated initializer:
NSIndexSet *catSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(5, 30)];
NSIndexSet *dogSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 15)];
buttonIndexByKind = [NSDictionary dictionaryWithObjectsAndKeys:
catSet, @"Cat", dogSet, @"Dog", nil
];
When you need to enable/disable your buttons:
NSIndexSet *toDisable = [buttonIndexByKind objectForKey:Dedicatedkind];
[toDisable enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop){
UIButton *btn = [allButtons objectAtIndex:idx];
btn.alpha=0;
btn.enabled=NO;
}];
Upvotes: 3
Reputation: 3937
you could do a generic getter for your variables, say something like:
- (id)getOAttack:(int)index {
switch (index) {
case 1:
return _oAttack1;
//etc
}
}
and then, whenever you need to set multiple properties, you can do like this
for (int i = 0; i < 30; i++) {
id oAttack = [self getOAttack:i];
oAttack.alpha = 0;
oAttack.enabled = NO;
}
Upvotes: 1
Reputation: 6032
Why don't you store them in array or something? You may even fill some array with these values and cycle through them when needed.
Upvotes: 0