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

时间:2017-07-07 11:08:17

标签: ios objective-c delegates selector

我最近学会了使用代理在按下按钮时将消息从一个类发送到另一个类,并且我想知道如何在没有按钮操作的情况下发送消息。 Apple documentation表示可能的方法是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'

有人可以解释我发送邮件需要做些什么吗?感谢。

结论

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

2 个答案:

答案 0 :(得分:1)

MicroTune.h文件中,

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

答案 1 :(得分:0)

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