在使用委托的ObjectiveC中,如何在不使用UIButton的情况下发送消息?

我最近学会了使用委托从一个类发送消息到另一个每当button被按下,我想找出如何发送消息没有button的动作。 苹果文档build议一个可能的方法是performSelector:(SEL)aSelector; 但是当我尝试使用它时,我没有运气。 相反,这是我所尝试的。

MicroTune.h中 ,我定义了一个委托并给它一个属性

  @class Synth; @protocol TuningDelegate <NSObject> -(void)setTuning:(NSData *)tuningData; @end @interface MicroTune : NSObject { … } @property (assign) id<TuningDelegate> delegate; @end 

Synth.h中 ,我声明了这个类作为委托

  #import "MicroTune.h" @interface Synth : NSObject <TuningDelegate> 

Synth.m中 ,我创build了一个让我知道消息到达的方法

  #import "Synth.h" - (void)setTuning:(NSData *)tuningData { NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:tuningData]; NSLog(@" hip hip %@", array); } 

编辑


而且,在Synth.m中,为了确保代理被识别,我添加了以下内容

  - (id)initWithSampleRate:(float)sampleRate_ { if ((self = [super init])) { microTuneClassObject.delegate = self; // etc. etc. } return self; } (Use of undeclared identifier 'microTuneClassObject') 

也试过了

  MicroTune.delegate = self; (Property 'delegate' not found on object of type 'MicroTune') 

  self.MicroTune.delegate = self; (Property 'MicroTune' not found on object of type 'Synth *') 

最后,在MicroTune.m中 ,我定义了一个发送消息的方法

  #import "MicroTune.h" - (void)sendTuning:(NSData *)tuningData { [synthLock lock]; [self.delegate setTuning:(NSData *)tuningData]; [synthLock unlock]; } 

但Xcode给了以下消息。

  No type or protocol named 'TuningDelegate' 

有人可以解释我需要做什么来发送消息吗? 谢谢。


结论

解决方法可以在我的补充答案中find。

MicroTune.h文件中,

而不是#import "Synth.h"@class Synth

Synth.h中

  @class MicroTune; @interface Synth : NSObject { MicroTune *setTuning; } - (MicroTune*)setTuning; 

Synth.m

  #import "Synth.h" #import "MicroTune.h" 

并从Synth.m,调优数据从MicroTune中检索(当PlayViewController发送一个MIDI程序改变消息)

  - (void)sendProgramChange:(uint8_t)oneOfFiveFamilies onChannel:(uint8_t)oneOfSixteenPlayers { uint8_t tuningTransposition = oneOfFiveFamilies; uint8_t assignedPitches = oneOfSixteenPlayers; MicroTune *dekany = [[MicroTune alloc] init]; // send a 2-byte "MIDI event" to fetch archived tuning data id archivedArray = [dekany sendMIDIEvent:(uint8_t)tuningTransposition data1:(uint8_t)assignedPitches]; NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:archivedArray]; NSLog(@"\n%@", array); for (int i = 0; i <10; i++) { NSNumber *num = array[i]; float hertz = [num floatValue]; _pitches[i] = hertz; } }