使用MyAppDelegate时无法识别的选择器

时间:2014-07-26 17:00:25

标签: objective-c

我已按照说明(我相信)Use your app delegate to share info between objects

但我一直收到以下错误:

  

[AppDelegate setBackgroundAudio:]:发送到的无法识别的选择器   实例0x10d822400    * 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [AppDelegate   setBackgroundAudio:]:发送到实例的无法识别的选择器   0x10d822400'

MyAppDelegate.h

@interface MyAppDelegate : NSObject{
    AVAudioPlayer *backgroundAudio;
}

@property (strong, nonatomic) AVAudioPlayer *backgroundAudio;

@end

MyAppDelegate.m

@implementation MyAppDelegate
@synthesize backgroundAudio;
@end

ViewController.m

MyAppDelegate *app_delegate = (MyAppDelegate*) [UIApplication sharedApplication].delegate;

backgroundAudioPath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];

backgroundAudioURL = [NSURL fileURLWithPath:backgroundAudioPath];
app_delegate.backgroundAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundAudioURL error:Nil];

[app_delegate.backgroundAudio setDelegate:self];
[app_delegate.backgroundAudio setNumberOfLoops:-1];
[app_delegate.backgroundAudio prepareToPlay];

if (app_delegate.backgroundAudio != nil){
    [app_delegate.backgroundAudio stop];
    [app_delegate.backgroundAudio setCurrentTime:0];
    [app_delegate.backgroundAudio play];
}

1 个答案:

答案 0 :(得分:1)

您已将MyAppDelegate作为NSObject的子类,因此您的应用代表绝不会(除了它的名字)。

当你获得真正的应用程序委托并将其转换为MyAppDelegate时,你现在已经得到了一个对象,你已经告诉编译器实现了backgroundAudio,但它并没有。快速补救措施是将MyAppDelegate重新声明为UIResponder的子类,并实施<UIApplicationDelegate>协议。

请务必告诉您的应用主要内容,例如:

// main.m
// all the usual stuff from your main.m

return UIApplicationMain(argc, argv, nil, NSStringFromClass([MyAppDelegate class]));