我最近学会了在按下按钮时使用委托将消息从一个类发送到另一个类,我想了解如何在没有按钮操作的情况下发送消息。 Apple 文档建议一种可能的方法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中,我创建了一个方法让我知道消息到达了
#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'
有人可以解释我需要做什么才能发送消息吗?谢谢。
结论
解决方案可以在我的补充答案中找到。
在MicroTune.h
文件中,
而不是#import "Synth.h"
写@class Synth
InSynth.h
@class MicroTune;
@interface Synth : NSObject
{
MicroTune *setTuning;
}
- (MicroTune*)setTuning;
在合成器中
#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;
}
}