在使用委托的ObjectiveC中,如何在不使用UIButton的情况下发送消息? [英] In ObjectiveC using delegate how do I send a message without using a UIButton?
问题描述
我最近学会了只要按下按钮,就可以使用委托将消息从一个类发送到另一类,并且我想了解如何在不执行按钮操作的情况下发送消息. Apple文档提示一种可能的方法是 performSelector:(SEL)aSelector;
,但是尝试使用它时我没有运气.相反,这是我尝试过的.
I recently learned to use delegates to send messages from one class to another whenever a button is pressed and I'd like to find out how to send a message without a button action. Apple documentation suggests a likely method is performSelector:(SEL)aSelector;
but I had no luck when I tried to use it. Instead, this is what I tried.
在 MicroTune.h 中,我定义了一个委托并为其赋予了属性
In MicroTune.h, I defined a delegate and gave it a property
@class Synth;
@protocol TuningDelegate <NSObject>
-(void)setTuning:(NSData *)tuningData;
@end
@interface MicroTune : NSObject
{
…
}
@property (assign) id<TuningDelegate> delegate;
@end
在 Synth.h 中,我声明了该类,以便其充当委托人
In Synth.h, I declared the class so it acts as a delegate
#import "MicroTune.h"
@interface Synth : NSObject <TuningDelegate>
在 Synth.m 中,我创建了一种方法,让我知道消息已到达
and in Synth.m, I created a method to let me know the message arrived
#import "Synth.h"
- (void)setTuning:(NSData *)tuningData
{
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:tuningData];
NSLog(@" hip hip %@", array);
}
编辑
而且,同样在Synth.m中,为了确保能够识别出该委托人,我添加了以下内容
And, also in Synth.m, to make sure the delegate is recognised, I added the following
- (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 中,我定义了一种发送消息的方法
And finally, in MicroTune.m, I defined a method to send the message
#import "MicroTune.h"
- (void)sendTuning:(NSData *)tuningData
{
[synthLock lock];
[self.delegate setTuning:(NSData *)tuningData];
[synthLock unlock];
}
但是Xcode给出了以下消息.
but Xcode gave the following message.
No type or protocol named 'TuningDelegate'
有人可以解释一下我发送邮件需要做什么吗?谢谢.
Can someone kindly explain what I need to do to send the message ? Thanks.
结论
该解决方案可以在我的补充答案中找到.
The solution can be found in my supplementary answer.
推荐答案
在 MicroTune.h
文件中,
代替 #import"Synth.h"
编写 @class Synth
这篇关于在使用委托的ObjectiveC中,如何在不使用UIButton的情况下发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!