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];
}
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: