Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-05-2010, 09:54 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Wien/Austria
Posts: 242
StefanL is on a distinguished road
Default NSMutableArray in Subclass: HowTo

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;
    }
    
}
StefanL is offline   Reply With Quote
Old 12-05-2010, 10:18 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Can you make selectedWinzis an instance variable for this class? Then you can create it once in the init.

If that won't work because you have multiple instances of this class that need to share the same selectedWinzis then you could declare it as "static" inside that function - then it would only get created once, and would always be available inside that function. There's no way to release it though, so don't abuse that technique.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 12-05-2010, 01:55 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Wien/Austria
Posts: 242
StefanL is on a distinguished road
Default

Hi Smasher,

thanks a lot! You were absolut right:

Code:
-(NSMutableArray*) selectedWinzis
{
    static NSMutableArray* theArray = nil;
    if (theArray == nil)
    {
        theArray = [[NSMutableArray alloc] init];
    }
    return theArray;
}
Thanks and BR,

Stefan
StefanL is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 385
6 members and 379 guests
chemistry, daudrizek, HemiMG, jeroenkeij, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:54 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0