ios两种方式自定义委托(delegate)

标签 ios objective-c multithreading delegates

我有两个类AB 。我有一个删除来传递 A 中的变量至 B 。我尝试让另一位来自 B 的委托(delegate)至 A ,这样我就可以控制A来自 B 。我尝试创建另一个委托(delegate),但它不起作用。由于我已经在 B.h 中导入了 A.h,因此无法在 A.h 中导入 B.h。我应该怎么做才能设置两种方式的自定义委托(delegate)?

啊啊

@protocol SoundDetectDelegate <NSObject>
-(void)update_from_sound: (SKSpriteNode*) node;
@end
@interface SoundDetect : NSObject
@property (nonatomic, weak) id <SoundDetectDelegate> delegate;
+ (id)sharedInstance;
-(void)beat;
@end

B.h

@interface HelloScene : SKScene <SoundDetectDelegate>
@property(nonatomic, strong) SoundDetect* sd;
@property(nonatomic, strong)Song* song;
@end

这是单向委托(delegate)。

我尝试过类似 B.h 的内容:

@protocol SceneDelegate <NSObject>
-(void)cancel_thread;
@end
@interface HelloScene : SKScene <SoundDetectDelegate>
@property(nonatomic, strong) SoundDetect* sd;
@property(nonatomic, strong)Song* song;
@end

但我不能有@interface SoundDetect : NSObject <SceneDelegate>在啊

B 传递变量的主要目的至 A是我想发送一个变量到 A ,所以 A可以取消其当前线程。

谢谢

最佳答案

您需要做的是使用返回类型为 BOOL 的 block 来通知是否应该取消它,而不是使用委托(delegate)。

关于ios两种方式自定义委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28059813/

相关文章:

objective-c - Objective-C中的实例消息错误,这是什么意思?为什么会这样呢?

java - 如何在使用 postDelayed() 循环的 Android 中停止 Runnable?

ios - 如何将 Parse PFFile 从一个应用程序移动到另一个应用程序?

ios - UNUserNotificationCenterDelegate 和 didReceiveRemoteNotification 的区别

ios - SpriteKit 网络物理(各种设备)

c++ - 使用 soundtouch 库在 iOS 中检测 BPM

ios - dequeueReusableCellWithReuseIdentifier 创建一个没有 subview 的单元格

java - 如何创建用户定义的线程数?

c - 多线程与单线程进程运行时

ios - Swift Combine 属性继承在 Xcode 11.4 beta 2/iOS 13.4 上抛出 'Fatal error: Call of deleted method'