class和viewController.m之间的代理

时间:2012-06-04 02:41:19

标签: objective-c ios delegates

我有PGMidi.h以下代理人

@protocol PGMidiSourceDelegate
- (void) midiSource:(PGMidiSource*)input midiReceived:(const MIDIPacketList *)packetList;

然后在我的viewController.m中,我只想在调用委托时获得NSLog

@interface viewController () <PGMidiDelegate, PGMidiSourceDelegate>

@end

@implementation viewController;

- (void) midiSource:(PGMidiSource*)midi midiReceived:(const MIDIPacketList *)packetList   
{
  NSLog(@"test");
}

我知道代表正在工作,因为在PGMidi班级中我也放了

- (void) midiSource:(PGMidiSource*)midi midiReceived:(const MIDIPacketList *)packetList  
{
 NSLog(@"test");
}

它有效。

但由于某种原因,它没有与viewController.m通信。我也在标题中声明了@PGMidi类。但也许我必须导入整个PGMidi.h?

2 个答案:

答案 0 :(得分:1)

在您的PGMidi.h中,您实际上应该声明一个委托属性,即

@property (nonatomic, assign) id<PGMidiSourceDelegate> delegate;

确保PGMidi.m文件中的synthesize该属性。然后,在你的PGMidi.m中你应该这样做:

-(void) midiSource:(PGMidiSource*)midi midiReceived:(const MIDIPacketList *)packetList {
    [delegate midiSource:midi midiReceived:packetList];
}

您还需要将视图控制器实际设置为PGMidi对象的委托:

myPGMidi.delegate = myViewController;

答案 1 :(得分:0)

是否包含@end在PGMidi.h中结束代码@protocol PGMidiSourceDelegate?还要在viewController.h中确保你说@interface viewController : (parentClass) <PGMidiSourceDelegate。 (parentClass)是viewController继承的任何类。基本上,要么你的问题是忘了@end,要么你没有指定viewController是PGMidi.h的委托。