简体   繁体   English

如何使用此委托中定义的方法?

[英]How can I use methods defined in this delegate?

I'm new to iOS development, and I'm playing with an interface trying to understand some concepts. 我是iOS开发的新手,并且正在尝试尝试了解一些概念的界面。 The provided SDK(which is compiled and I can't do anything to it) has the following definitions: 提供的SDK(已编译,我对此无能为力)具有以下定义:

@class HRMonitor;
@protocol HRMonitorDelegate
- (void) hrmon: (HRMonitor*) mon heartRateUpdate: (double) hr;
// And others
@end

@interface HRMonitor : NSObject <NSStreamDelegate>{
}

-(id) init: (id) _delegate;
-(void)startup;

Does anyone have idea how can I use the heartRateUpdate method defined in the protocol HRMonitorDelegate ? 有谁知道如何使用协议HRMonitorDelegate定义的heartRateUpdate方法? From what I read in the iOS Developer Library, I have to have an interface that conforms to the Delegate like HRMonitor : NSObject <HRMonitorDelegate> to call methods in the protocol. 从我在iOS开发人员库中阅读的内容来看,我必须具有一个符合Delegate的接口,例如HRMonitor : NSObject <HRMonitorDelegate>才能调用协议中的方法。 But that's not provided in the API. 但这未在API中提供。

Or can I use the init method? 还是可以使用init方法? But then how should I pass the _delegate ? 但是,我该如何传递_delegate呢?

  1. conform your interface to the delegate 使您的界面与委托人保持一致

  2. init HRMonitor, passing your interface instance as the _delegate 初始化HRMonitor,将您的接口实例作为_delegate传递

  3. then the - (void) hrmon: (HRMonitor*) mon heartRateUpdate: (double) hr of you interface will be called 然后会调用-(void)hrmon:(HRMonitor *)mon heartRateUpdate:(double)hr

  4. make a interface conforms to the delegate, and call the method of it when you need, remember to check the delegate is not nil and response to the method you want to call 使接口符合委托,并在需要时调用该接口的方法,请记住检查委托是否为nil并响应要调用的方法

     @interface YourClass : NSObject <HRMonitorDelegate> @implementation HRMonitor -(void)someMethod { HRMonitor monitor = [HRMonitor alloc] init:self]; } - (void) hrmon: (HRMonitor*) mon heartRateUpdate: (double) { } 

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

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