【问题标题】:Unrecognized selector when using MyAppDelegate使用 MyAppDelegate 时无法识别的选择器
【发布时间】:2014-07-26 17:00:25
【问题描述】:

我已按照指示(我相信)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];
}

【问题讨论】:

    标签: objective-c


    【解决方案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]));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多