NSMutableArray Noob Prob

Discussion in 'iPhone/iPad Programming' started by Yoda101B, Mar 7, 2009.

  1. macrumors newbie

    Joined:
    Mar 7, 2009
    #1
    Hi guys,

    I'm tearing my hair out! I know I'm missing something silly but I just can work it out - any help would be very gratefully received!

    Basically I have a class 'Node'


    Code:
    @interface Node : NSObject {
    
    	NSString	*name;
    	
    	NSMutableArray		*inLets;
    
            UIImageView  *view;
    }
    
    @property (nonatomic, retain)	NSMutableArray	*inLets;
    @property (nonatomic, retain)	UIImageView  *view;
    @end 

    Node.M

    Code:
    @implementation Node
    
    @synthesize name, isRunning, hasError,inLets;
    
    -(id) init {
    	
    	if (self = [super init]) {
    		
    		hasError = NO;
    		
    		self.inLets = [NSMutableArray array];
    		self.outLets = [[NSMutableArray alloc] init];
    		
    	}
    	return self;	
    }
    
    

    And then I have a MainViewController to display the nodes, and remembers which one is selected...
    Code:
    @interface MainViewController : UIViewController {
    
    
    		NodeView					*currentSelection;
    	
    		
    	}
    
    @property (nonatomic, retain) NodeView *currentSelection;
    
    
    MainViewController.M

    Code:
     -(void)viewDidLoad {
    
    	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nodeSelected:object:) name:@"nodeselected" object:nil];
    
    }
    
    -(void) nodeSelected:(NSNotification *)aNotification object:(id)object{
    	
    	if (nodeSelected != nil) {
    			nodeSelected.isSelected = NO; 
    	} else {
    		
    		nodeSelected = [[NodeView alloc] init];
    	}
    	
    	NodeView *NewCurrentSelection = [[NodeView alloc] init];
    	NewCurrentSelection = [aNotification object];
    	
    	nodeSelected = nil;
    	nodeSelected = NewCurrentSelection;
    	nodeSelected.isSelected = YES;
    	
    	
    	bottomHudViewController.nameLabel.text = nodeSelected.owner.name;
    	
    	[bottomHudViewController showBottomHudView:(YES)];
    }
    
    The NodeView class inherits from the UIImageView class and just posts a Notification when it's clicked on....


    My problem is to do with NewCurrentSelection, for some reason no matter what I do the debugger always sees the inLets array changing to a CALayerArray - which obviously no good to me?

    What am I doing wrong????


    Thanks very much in advance!!

    Yoda
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What is the relationship between Node and NodeView? Because the top part of your posting shows us the definition of the class Node and then the MainViewController only refers to NodeView.
     
  3. thread starter macrumors newbie

    Joined:
    Mar 7, 2009
    #3
    In MainViewController.M

    Code:
    - (void) createNodeViewFromNode:(Node *)node {
    	
    	NodeView *nodeView = [[NodeView alloc] initWithImage: [UIImage imageNamed:@"NodeLozNormal.png"]]; 
    	[nodeView setUserInteractionEnabled:YES]; 
    		
    	node.view = nodeView;
    	nodeView.owner = node;
    	
    	
    	[self.view addSubview:nodeView]; 
    	
    	[nodeView release];
    	
    }
    But I'm not sure if thats related to my problem.... Thanks in advance
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Also, these lines of code seem curious (and are the cause of a memory leak):
    Code:
    NodeView *NewCurrentSelection = [[NodeView alloc] init];
    NewCurrentSelection = [aNotification object];
    You instantiate a new NodeView and then throw that away as you assign it aNotification's object.
     
  5. thread starter macrumors newbie

    Joined:
    Mar 7, 2009
    #5
    Yeah they are a bit odd and have changed to...

    Code:
    if (nodeSelected != nil) {
    			nodeSelected.isSelected = NO; 
    	} 
    	
    	[nodeSelected release];
    	
    	nodeSelected = [[aNotification object] retain];
    	
    	nodeSelected.isSelected = YES;
    

    I think thats right... to behonest I'm still scratching my head over when I should retain, or copy....

    I've finally sorted out my other problem (after a long night!!) Looks like I accidently was alloc++init the array twice!

    Thanks Though....
     

Share This Page