Reputation: 1987
I have 2 UIViewControllers, GameController and RhythmController. I defined RhythmDelegate in RhythmController.h like this. doEndGame method in RhythmController is supposed to call the delegate method in GameController gameOver, which implements the delegate protocol.
I can't understand why this is not working... is my code in error? Thanks in advance for any help.
@class RhythmView,RhythmDelegate, RhythmController;
@protocol RhythmDelegate
@required
-(void)gameOver:(RhythmController*)aRhythmController;
@end
@interface RhythmController : UIViewController <UIGestureRecognizerDelegate, UIAlertViewDelegate> {
.....
}
@property (nonatomic, retain) IBOutlet NSObject<RhythmDelegate>* delegate;
Within RhythmController.m I have the delegate call going like this :
@implementation RhythmController
@synthesize delegate;
-(void)doEndGame{
isPaused = true;
[delegate gameOver:self];
}
I confirm that the doEndGame is called with a breakpoint inside. However, it doesn't go to the method I want in GameController, which implements the delegate.
#import <UIKit/UIKit.h>
#import "CoinsController.h"
#import "CoinsGame.h"
#import "HighscoreController.h"
#import "RhythmController.h"
#import "RhythmView.h"
@interface GameController : UIViewController <RhythmDelegate> {
IBOutlet UIView *landscapePlayView;
IBOutlet UIView *landscapeGameHolderView;
IBOutlet UIView *portraitPlayView;
IBOutlet UIView *portraitGameHolderView;
IBOutlet UIView *loadingView;
IBOutlet UIView *welcomeView;
IBOutlet UIButton *continueButton;
IBOutlet UIView *aboutView;
IBOutlet UIView *playView;
IBOutlet UIView *rhythmView;
//IBOutlet UIView *levelSelectView;
IBOutlet UILabel *musicNotation;
IBOutlet CoinsController *coinsController;
IBOutlet RhythmController *rhythmController;
IBOutletCollection(UILabel) NSArray *remainingTurnsLabels;
IBOutletCollection(UILabel) NSArray *scoreLabels;
IBOutlet HighscoreController *highscoreController;
CoinsGame* previousGame;
BOOL isPlayingGame;
}
-(void)setPreviousGame:(CoinsGame*)aCoinsGame;
-(CoinsGame*)currentGame;
- (IBAction)continueGameClicked:(id)sender;
- (IBAction)newGameClicked:(id)sender;
- (IBAction)highScoresClicked:(id)sender;
- (IBAction)aboutClicked:(id)sender;
- (IBAction)aboutDoneClicked:(id)sender;
- (IBAction)highscoreDoneClicked:(id)sender;
-(void)loadImages;
-(void)showPlayView: (UIInterfaceOrientation)interfaceOrientation;
-(void)doPause;
@end
In GameController.m , the method is
// RhythmDelegate Tasks
-(void)gameOver:(RhythmController*)aRhythmController{
NSLog(@"Came into gameOver delegated task");
[self.view addSubview: welcomeView];
}
Upvotes: 1
Views: 1812
Reputation: 17732
You probably forgot to set the GameController
instance as the RhythmController
's delegate. Simply implementing the protocol for a delegate doesn't magically make the delegate connections. You need a line like this somewhere:
myRhythmController.delegate = myGameController;
As a note: delegates are typically a weak
reference, or are assign
properties. This is to prevent retain loops between the delegate object and the original object
Upvotes: 1
Reputation: 6065
Check if your delegate variable is nil at your break point. if yes then you forgot to setting the delegate.
You can easily set it in you viewdidload method of gamecontroller after you have created your RhythmController this:
rythmcontroller.delegate = self;
Upvotes: 2
Reputation: 2703
Didn't you forgot to set the delegate property for the rhythmController instance?
If you would have created the code you would have done somehting like this:
rhythmController = [[RhythmController alloc] init];
rhythemController.delegate = self;
...
[rhythmController release];
Where "self" is the GameController instance.
Upvotes: 1