0

I have the PGMidi.h with the following delegate

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

Then in my viewController.m I simply want to get an NSLog when the delegate is called so

@interface viewController () <PGMidiDelegate, PGMidiSourceDelegate>

@end

@implementation viewController;

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

I know that the delegate is working because in the PGMidi Class I also put

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

and it works.

But for some reason it is not communicating with the viewController.m. I am also declaring the @PGMidi class in the header. But perhaps I have to import the entire PGMidi.h?

Hector
  • 3,909
  • 2
  • 30
  • 46
frankie
  • 661
  • 2
  • 10
  • 25

2 Answers2

1

In your PGMidi.h you should actually declare a delegate property, i.e.

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

Make sure to synthesize that property in your PGMidi.m file. Then, in your PGMidi.m you should be doing this:

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

You also need to actually set the view controller as the delegate of your PGMidi object:

myPGMidi.delegate = myViewController;
Michael Frederick
  • 16,664
  • 3
  • 43
  • 58
  • 1
    I've always been curious about this; what does `assign` mean? I've read apple docs and other questions but I can't quite grasp its meaning. – pasawaya Jun 04 '12 at 03:05
  • 1
    assign represents a weak reference -- in this case, since your view controller is already retained in memory, we do not need to increase the retain count of the object by making it a strong property. In many cases, if you don't use a weak reference in a delegate setup you can create a retain cycle. check out http://stackoverflow.com/questions/918698/why-are-objective-c-delegates-usually-given-the-property-assign-instead-of-retai – Michael Frederick Jun 04 '12 at 05:52
0

Do you include @end to conclude your code @protocol PGMidiSourceDelegate in PGMidi.h? Also make sure in viewController.h that you say @interface viewController : (parentClass) <PGMidiSourceDelegate. (parentClass) is whatever class viewController inherits from. Basically, either your problem is you forgot the @end or you didn't specify that viewController is a delegate of PGMidi.h.

pasawaya
  • 11,515
  • 7
  • 53
  • 92