1

我有 2 个 UIViewControllers、GameController 和 RhythmController。我像这样在RhythmController.h中定义了 RhythmDelegate 。RhythmController 中的 doEndGame 方法应该调用 GameController gameOver 中的委托方法,它实现了委托协议。

我不明白为什么这不起作用......我的代码有错误吗?提前感谢您的帮助。

@class RhythmView,RhythmDelegate, RhythmController;

@protocol RhythmDelegate
@required
-(void)gameOver:(RhythmController*)aRhythmController;
@end

@interface RhythmController : UIViewController <UIGestureRecognizerDelegate, UIAlertViewDelegate> {
.....

}

@property (nonatomic, retain) IBOutlet NSObject<RhythmDelegate>* delegate;

在 RhythmController.m 中,我的委托调用如下:

@implementation RhythmController
@synthesize delegate;

-(void)doEndGame{

isPaused = true;
[delegate gameOver:self];
}

我确认 doEndGame 被调用,里面有一个断点。但是,它没有转到我在 GameController 中想要的方法,它实现了委托。

#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

在 GameController.m 中,方法是

// RhythmDelegate Tasks
-(void)gameOver:(RhythmController*)aRhythmController{

NSLog(@"Came into gameOver delegated task"); 
[self.view addSubview: welcomeView];
}
4

3 回答 3

2

检查您的委托变量是否在您的断点处为零。如果是,那么您忘记设置委托。

创建 RhythmController 后,您可以在 gamecontroller 的 viewdidload 方法中轻松设置它:

rythmcontroller.delegate = self;
于 2012-08-08T14:58:13.963 回答
1

您可能忘记将GameController实例设置为RhythmController的委托。简单地为委托实现协议并不能神奇地建立委托连接。你在某处需要这样的一行:

myRhythmController.delegate = myGameController;

注意:委托通常是weak引用,或者是assign属性。这是为了防止委托对象和原始对象之间的保留循环

于 2012-08-08T14:58:02.450 回答
1

你是不是忘记给节奏控制器实例设置委托属性了?

如果你已经创建了代码,你会做这样的事情:

rhythmController = [[RhythmController alloc] init];
rhythemController.delegate = self;
...
[rhythmController release];

其中“self”是 GameController 实例。

于 2012-08-08T14:58:10.623 回答