NSSoundDelegate未被调用

时间:2017-11-21 20:14:09

标签: objective-c audio

我正在更新一些遗留目标C代码,以便能够在OSX 10.13下编译。遗留代码有效,大多数更新代码也可以,除了需要处理didFinishPlaying函数的NSSoundDelegate。委托方法未被调用。委托方法包含在名为MyClass的类中。这是相关的代码。

在MyClass.h中:

@class MyClass;
@protocol MyClass <NSObject>
@optional
- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)flag;
@end

@interface MyClass : NSObject <NSSoundDelegate>
{
}
@property (nonatomic, assign) id <NSSoundDelegate> delegate;

- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)flag;
- (id) init;
@end

然后在MyClass.m中:

@implementation MyClass

@synthesize delegate;

- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)flag
{
if (flag) {
    [[NSNotificationCenter defaultCenter]   postNotificationName:@"handleNSSoundDidFinishPlaying" object:sound];
}
}

- (id)init
{
    MyClass *thePointer;

    self = [super init];
    if (self) {
        thePointer = self;
        self.delegate = (id)thePointer;
        isInitialized = NO;
        isClosing = NO;
        [self set_currentSounds:[NSMutableArray arrayWithCapacity:0]];
    }
    return self;
}
@end

任何人都能看到我失踪的东西吗?

2 个答案:

答案 0 :(得分:0)

我认为您应该通知委托对象,如:

if([_delegate respondsToSelector:@selector(sound: didFinishPlaying:)])
    [_delegate sound:self didFinishPlaying:_flag];

希望这会对你有所帮助。

答案 1 :(得分:0)

发现问题了!在分配要播放的声音时,您必须使用[theSnd setDelegate:self]设置声音委托;所以当声音停止播放时,委托被调用,在这种情况下委托是在MyClass .m文件中。