我正在开发一款游戏,我有一个以模态方式推送的设置视图。我希望如果我在该视图中关闭音乐,请停止背景音乐。我正在使用协议(我正在使用自动引用计数):
game.h
#import <UIKit/UIKit.h>
#import "AVFoundation/AVFoundation.h"
#import "stackButton.h"
#import "tvr_AppDelegate.h"
@protocol settingsChanger <NSObject>
-(void)changeSettings:(int)soundVal musicSetting:(int)musicVal;
@end
@interface game : UIViewController <UIAlertViewDelegate, settingsChanger>{
...
}
game.m
-(void)changeSettings:(int)soundVal musicSetting:(int)musicVal{
if (musicVal == 1) {
[self playMusic];
}else{
[audioPlayer stop];
}
settings.h
#import <UIKit/UIKit.h>
@protocol settingsChanger;
@interface settings : UIViewController{
id<settingsChanger> settingsDelegate;
}
@property (nonatomic, strong) id<settingsChanger> settingsDelegate;
-(IBAction)updateMusicSetting;
@end
settings.m
-(IBAction)updateMusicSetting{
tvr_AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
if (swMusic.on) {
[delegate updateMusicSetting:1];
[settingsDelegate changeSettings:0 musicSetting:1];
}
else {
[delegate updateMusicSetting:0];
[settingsDelegate changeSettings:0 musicSetting:0];
}
}
我的IBAction由UISwitch触发。一切顺利,除了id settingsDelegate引用内存0x0,我认为这是我的方法changeSettings永远不会在game.h中调用。我错过了什么?。
答案 0 :(得分:1)
我建议作为一个很好的编程习惯,用大写命名你的类,即:“游戏”而不是“游戏”和你的协议:“SettingsChanger”而不是“settingsChanger”。你的方法命名约定似乎也需要一些改进,只花30分钟时间阅读苹果“Cocoa Guidelines for Cocoa”link to the "Cocoa Guidelines For Cocoa document"