在使用的ObjectiveC委托我怎么发送邮件,而无需使用一个UIButton?(In Object

2019-10-30 03:11发布

我最近学会了使用委托只要按下一个按钮,我想找出如何发送消息没有按钮动作,从一类将消息发送到另一个。 苹果文档提出了一种可能的方法是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' 

可有人好心解释什么,我需要做什么来发送邮件? 谢谢。


结论

该解决方案可以在我的补充回答中找到。

Answer 1:

MicroTune.h文件,

代替#import "Synth.h"@class Synth



Answer 2:

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;
        }
    }


文章来源: In ObjectiveC using delegate how do I send a message without using a UIButton?