在使用委托的 ObjectiveC 中,如何在不使用 UIButton 的情况下发送消息?
In ObjectiveC using delegate how do I send a message without using a UIButton?
我最近学会了使用代理在按下按钮时将消息从一个 class 发送到另一个,我想了解如何在没有按钮操作的情况下发送消息。 Apple documentation 表明一种可能的方法是 performSelector:(SEL)aSelector;
但我尝试使用它时运气不佳。相反,这就是我尝试过的。
在MicroTune.h中,我定义了一个delegate,给它一个属性
@class Synth;
@protocol TuningDelegate <NSObject>
-(void)setTuning:(NSData *)tuningData;
@end
@interface MicroTune : NSObject
{
…
}
@property (assign) id<TuningDelegate> delegate;
@end
在 Synth.h 中,我声明了 class,因此它充当委托
#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
在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;
}
}
我最近学会了使用代理在按下按钮时将消息从一个 class 发送到另一个,我想了解如何在没有按钮操作的情况下发送消息。 Apple documentation 表明一种可能的方法是 performSelector:(SEL)aSelector;
但我尝试使用它时运气不佳。相反,这就是我尝试过的。
在MicroTune.h中,我定义了一个delegate,给它一个属性
@class Synth;
@protocol TuningDelegate <NSObject>
-(void)setTuning:(NSData *)tuningData;
@end
@interface MicroTune : NSObject
{
…
}
@property (assign) id<TuningDelegate> delegate;
@end
在 Synth.h 中,我声明了 class,因此它充当委托
#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
在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;
}
}