views:

41

answers:

1

Hi guys,

My main view PlayGameViewController has a subview (actually 2 of them) called CardViewController.

CardViewController creates some buttons programmatically

  -(void) initialiseButtons 
  {
    NSLog(@"initialiseButtons  %d",totalButtons);
    int ypos = playerImage.frame.origin.y + playerImage.frame.size.height + 42;
    for(int i=0; i<totalButtons; i++) 
    {       
        StatView *sv = [[StatView alloc] initWithYPos:ypos];
        sv.tag = 100 + i;
        [sv.overlayButton addTarget:self action:@selector(statTapped:) 
                forControlEvents:UIControlEventTouchUpInside];
        sv.overlayButton.tag = 10 + i;
        [self.frontView addSubview:sv];
        ypos += 26;
    }
  }

It sets a callback function statTapped. This works ok and the function does get called. But... All the game logic is in PlayGameViewController so I need to handle the function there. I tried deleting the function from CardViewController and implementing it instead in the PlayGameViewController but the call wasn't passed down to the parent class.

Is there away to achieve this or am I talking and thinking crazy?

+1  A: 

I think you have a couple of options:

  1. Create a delegate to process your statTapped: method. You can learn more about delegates and how to create one here: http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c
  2. Use NSNotification and add a listener in PlayGameViewController that will call the statTapped: method (also defined in PlayGameViewController. Then in CardViewController have your selector action call a method that posts a notification. PlayGameViewController will receive that notification and then run the specified tasks.

Here is an example of using NSNotification:

In your PlayGameViewController's init method, write:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(statTapped:) 
                                             name:@"statTapped"
                                           object:nil];

In CardViewController, you'll want to set a selector to your button action as you do now, but instead of statTapped: you'll want a different method which contains this code:

[[NSNotificationCenter defaultCenter] postNotificationName:@"statTapped" 
                                                    object:self];

By doing so your PlayGameController will own the statTapped method.

Don't forget to remove the observer in your viewDidUnload method:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"statTapped" object:nil];
Calvin L
Ok i follow you as far as setting up the NSNotification center in PlayGameViewController. The second line is code : [[NSNotificationCenter defaultCenter] postNotificationName:@"statTapped" object:self]; Does that get called from within my startTapped method inside CardViewController? Then finally I implement a method called startTapped inside PlayGameViewController which will be called as a result of all the above code.
Code
Yes, you've got it.
Calvin L