简体   繁体   English

当UIButton未产生事件时,是否可以使用委托发送消息?

[英]Is it possible to use a delegate to send a message when the event is not produced by a UIButton?

I'd like to know how to use a delegate to send a clock message to MyViewController from a class called MotionListener but am having trouble translating Apple's explanation of target-action into a workable command. 我想知道如何使用委托从名为MotionListener的类向MyViewController发送时钟消息,但是在将Apple对target-action的解释转换为可行命令时遇到了麻烦。

Target-action is a design pattern in which an object holds the information necessary to send a message to another object when an event occurs. 目标动作是一种设计模式,其中,一个对象保存事件发生时向另一个对象发送消息所需的信息。 The stored information consists of two items of data: an action selector, which identifies the method to be invoked, and a target, which is the object to receive the message.] 1 存储的信息由两部分数据组成:一个动作选择器,它标识要调用的方法;一个目标,它是接收消息的对象。] 1

I have no trouble applying the explanation (above) in the example (below) with myButton using a delegate to send a message to a target called fromButtonInSubview whenever it is pressed 我毫不费力地将myButton的示例(下)中的解释(下)应用于myButton使用该fromButtonInSubview将消息发送到名为fromButtonInSubview的目标,只要按下该fromButtonInSubview

        [mySyncButton addTarget:self.delegate
                         action:@selector(fromSyncButtonInSubview:)
               forControlEvents:UIControlEventTouchUpInside];

I would assume that in order to send a signal that does not originate from a UIButton, I would still need to declare the protocol [a] , give it a property [c] and include an initialisation method [b] 我假设为了发送不是源自UIButton,的信号UIButton,我仍然需要声明协议[a] ,为其指定一个属性[c]并包括一个初始化方法[b]

[a] [一种]

    @protocol SyncDelegate <NSObject>

    -(void)fromSync:(int)clock;

    @end

    @interface MotionListener : NSObject {

    }

[b] 并[b]

    - (id) initMotionSensingWith:(float)updateInterval;

[c] [C]

    @property (assign) id<SyncDelegate> delegate;

    @end

and declare the protocol in the interface [d] and add the target method in the implementation [e] 并在接口[d]中声明协议,并在实现[e]中添加目标方法

[d] 并[d]

    @interface PlayViewController : UIViewController <SyncDelegate>

[e] 并[e]

    - (void)fromSync:(int)clock
    {
        NSLog(@"tick"); // do stuff and show
    }

then in MyViewController, import MotionListener [f] , declare it in the implementation [g] and define the delegate [h] 然后在MyViewController,导入MotionListener [f] ,在实现[g]中对其进行声明,并定义委托[h]

[f] [F]

    #import "MotionListener.h"

[g] [G]

    MotionListener *sync = [[MotionListener alloc] initMotionSensingWith:(float)updateInterval];

[h] [H]

    sync.delegate = self;

But despite re-reading the explanation quoted above and after several failed attempts, I have yet to write a command that will send a sync signal from MotionListener to MyViewController. 但是,尽管重新阅读了上面引用的说明,并且在几次尝试失败之后,我仍未编写一个命令,该命令将从MotionListener发送同步信号到MyViewController. I know it will have the effect of addTarget:self.delegate and action:@selector(fromSync:) in the UIButton example (above) . 我知道它将在UIButton示例(如上)中具有action:@selector(fromSync:) addTarget:self.delegateaction:@selector(fromSync:)的效果。 And before anyone suggests NSTimer selector, I already clock this at 50 Hz whereas the sync signal is 1 Hz 在有人建议使用NSTimer selector,之前NSTimer selector,我已经以50 Hz的频率对其进行了计时,而同步信号为1 Hz

eg 例如

     [NSTimer scheduledTimerWithTimeInterval:0.02;  // 50 Hz
                                      target:self
                                    selector:@selector(motionRefresh:)
                                    userInfo:nil
                                     repeats:YES];

    - (void)motionRefresh:(id)sender
    {
        count = (count + 1) % 50;  // every 1 second

        if (count == 0 ) 
        {
            // send the sync message here
            // EDIT
            NSLog(@"sending %i", count);
            [self.delegate fromSync:(int)count];
        }
    }

So my question is: using a delegate how can I send a sync message each time count turns 0 ? 所以我的问题是:使用委托,每次count变为0时如何发送同步消息? The answer is probably embarrassingly simple but I can wear that if you can help. 答案可能很尴尬,但如果可以的话,我可以考虑。 Thanks. 谢谢。

I'm not sure if I understand your code correctly. 我不确定我是否正确理解您的代码。 but I think you simply have to replace your // send the sync message here line with a call to your delegate like [self.delegate fromSync:WHATEVERVALUEYOUWANTTOINFORMABOUT]; 但我认为您只需要用对您的代表的呼叫代替// send the sync message here行,例如[self.delegate fromSync:WHATEVERVALUEYOUWANTTOINFORMABOUT]; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM