Hi Folks,
in my app I am using a Subclass to verify the state of a UIButton. As this class is designed to only allow 1 active Button I want to hack it to have 2 Buttons active. Therefore I thought I am putting the selectedItem into a NSMutableArray and if the [NSMutableArray count] is larger then 2 then I have to unselect the Buttons.
When ccTouchBegan then I want to put the curSelection into my NSMutableArray, to unselect it when the count is two. How can I achieve this, without init the NSMutableArray everytime I call this class?
Thanks for any Feedback!
BR,
Stefan
Code:
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if ( state != kMenuStateWaiting ) return NO;
curSelection = [self itemForTouch:touch];
[curSelection selected];
_curHighlighted = curSelection;
NSLog(@"CurSelection: %@", curSelection);
NSMutableArray *selectedWinzis = [[NSMutableArray alloc] init]; //Problem is here
[selectedWinzis addObject:[NSString stringWithFormat:@"%@",curSelection ]];
NSLog(@"SelectedWinzis from Array: %@", selectedWinzis);
if (_curHighlighted) {
if (selectedItem != curSelection) {
if ([selectedWinzis count] == 2) {
[selectedItem unselected];
[[selectedWinzis objectAtIndex:1] unselected];
[[selectedWinzis objectAtIndex:0] unselected];
[selectedWinzis removeObjectAtIndex:1];
[selectedWinzis removeObjectAtIndex:0];
}
state = kMenuStateTrackingTouch;
return YES;
}
}
return NO;
}
The whole class:
Code:
- (void)setSelectedItem:(CCMenuItem *)item {
[selectedItem unselected];
selectedItem = item;
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if ( state != kMenuStateWaiting ) return NO;
curSelection = [self itemForTouch:touch];
[curSelection selected];
_curHighlighted = curSelection;
NSLog(@"CurSelection: %@", curSelection);
NSMutableArray *selectedWinzis = [[NSMutableArray alloc] init];
[selectedWinzis addObject:[NSString stringWithFormat:@"%@",curSelection ]];
NSLog(@"SelectedWinzis from Array: %@", selectedWinzis);
if (_curHighlighted) {
if (selectedItem != curSelection) {
if ([selectedWinzis count] == 2) {
[selectedItem unselected];
[[selectedWinzis objectAtIndex:1] unselected];
[[selectedWinzis objectAtIndex:0] unselected];
[selectedWinzis removeObjectAtIndex:1];
[selectedWinzis removeObjectAtIndex:0];
}
state = kMenuStateTrackingTouch;
return YES;
}
}
return NO;
}
- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
NSAssert(state == kMenuStateTrackingTouch, @"[Menu ccTouchEnded] -- invalid state");
curSelection = [self itemForTouch:touch];
if (curSelection != _curHighlighted && curSelection != nil) {
[selectedItem selected];
// [_curHighlighted unselected];
// _curHighlighted = nil;
state = kMenuStateWaiting;
return;
}
selectedItem = _curHighlighted;
[_curHighlighted activate];
_curHighlighted = nil;
state = kMenuStateWaiting;
}
- (void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event {
NSAssert(state == kMenuStateTrackingTouch, @"[Menu ccTouchCancelled] -- invalid state");
[selectedItem selected];
[_curHighlighted unselected];
_curHighlighted = nil;
state = kMenuStateWaiting;
}
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
NSAssert(state == kMenuStateTrackingTouch, @"[Menu ccTouchMoved] -- invalid state");
curSelection = [self itemForTouch:touch];
if (curSelection != _curHighlighted && curSelection != nil) {
[_curHighlighted unselected];
[curSelection selected];
_curHighlighted = curSelection;
return;
}
}