簡體   English   中英

在使用委托的ObjectiveC中,如何在不使用UIButton的情況下發送消息?

[英]In ObjectiveC using delegate how do I send a message without using a UIButton?

最近,我學會了使用委托在按下按鈕時將消息從一個類發送到另一類,並且我想了解如何在不執行按鈕操作的情況下發送消息。 蘋果文檔表明可能的方法是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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM