views:

55

answers:

1

This is probably going to be tough to explain.

Basically I have the following structure to my project:

I have an object (graph) which controls other objects (nodes) and (arcs).

Everything is working fine and dandy but I've found the need to add buttons to my node objects. This is working fine too, I have an NSLog output letting me know which node has been selected etc. My issue however is I only want one node selected at a time. The initialisation code for my node objects is as follows:

- (id) initNodeWithData:(NSString *) _label: (int) _xpos: (int) _ypos 
{
    if(self = [super init])
    {
    //other variable inits here...

    [self setButton: [UIButton buttonWithType:UIButtonTypeCustom]];

    [button setImage:[UIImage imageNamed:@"node.png"]
            forState:UIControlStateNormal];

    [button setImage:[UIImage imageNamed:@"node.png"]
            forState:UIControlStateHighlighted];

    [button setImage:[UIImage imageNamed:@"nodeSelected.png"]
            forState:UIControlStateSelected];

    [button setImage:[UIImage imageNamed:@"nodeSelected.png"]
            forState:(UIControlStateHighlighted|UIControlStateSelected)];

    [button addTarget:self 
               action:@selector(buttonPressed) 
     forControlEvents:UIControlEventTouchUpInside];

}
return self;
}

- (void)buttonPressed
{
    NSLog(@"%@ Pressed", label);

    if(button.selected == YES) button.selected = NO;
    else if(button.selected == NO) button.selected = YES;
}

Because my node objects are independent form each other I can't (I don't think I can anyway) test to see if any other nodes have been selected before proceeding with selection.

Ideally I want something like:

- (void)buttonPressed
{
    //Check to see if other nodes are selected
    //If not, button.selected = yes
    //else clear other buttons
    //then select current button
}

But I think I need this code in my viewController class, where all my other code is, and I don't know how to do it because I assume it's all out of scope.

Sorry that this isn't very clear, it's difficult to explain!

A: 

You could have some object that stores the currently selected node. If a user presses a button you send an NSNotification to that "master" object.

MasterObject.h

Node *currentlyActiveNode;

MasterObject.m

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(activateNode:) name:kNotificationActivateNode object:nil];

...

- (void)activateNode:(NSNotification *)notification {
   Node *requestingNode = (Node *)[notification.object];
   if (requestingNode != currentlyActiveNode) {
      [currentlyActiveNode deactivate];
      currentlyActiveNode = requestingNode;
      [requestingNode activate];
   }

}

Node.m

- (void)requestActivation {
    [[NSNotificationCenter defaultCenter] postNotificationName:name:kNotificationActivateNode object:self];
}

- (void)activate {
   //Whatever...
}
- (void)deactivate {
   //Whatever...
}

Your 'graph' could be your master object...

Alternative: You could even just have an NSNotification observer in every node that listens to a "deactivate" notification. Then what you would do if someone clicks a button is simply send that notification (which the nodes listen and react to by deactivating) and then activate the current node. To make sure those two events don't interfere, you could send a reference to the sending node, and not deactivate if sender == receiver.

Joseph Tura
Got it working! :) I think I did the alternative method you suggested, I'm very new to objective C so I had to do some snooping around for NSNotification examples and how to use it but now it works great! :) Thank you very much for your help, kudos to you sir!
Zachary Markham