【问题标题】:ios two ways custom delegateios 两种方式自定义委托
【发布时间】:2023-03-06 01:28:01
【问题描述】:

我有两个课程A 和B。我有一次删除将变量从A 传递到B。我尝试让另一个代表从B 到A,这样我就可以从B 控制A。我试图创建另一个委托,但它不起作用。由于我已经在 B.h 中导入了 A.h,因此我无法在 A.h 中导入 B.h。我应该怎么做才能设置两种方式自定义委托?

啊.h

@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

这是一种委托方式。

我试图在 B.h 中拥有类似的东西:

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

但我不能在 A.h. 中拥有 @interface SoundDetect : NSObject &lt;SceneDelegate&gt;

将变量从B 传递到A 的主要目的是我想将变量发送到A,以便A 可以取消其当前线程。

谢谢

【问题讨论】:

  • 你想要什么类型的控制,或者你想从 B 到 A 具体做什么?

标签: ios objective-c multithreading delegates


【解决方案1】:

不用委托,你需要做的是使用一个返回类型为 BOOL 的块来通知它是否应该被取消。

【讨论】:

    【解决方案2】:

    1-为了将数据从 A 传递到 B,您可以使用 Segue,当调用它的委托时,您可以像这样将数据从 A 传递到 B

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
     {
        if([segue.identifier isEqualToString:@"segueFromFirstToSecond"])
       {
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        SecondViewController *controller = (SecondViewController *)navController.topViewController;
        controller.myValue = YES;  /// myvalue is property in secondViewCOntroller
       }
     }
    

    2-为了将数据从 B 传递到 A,您可以点击这些链接

    http://www.mysamplecode.com/2012/12/ios-passing-data-between-view-controllers.html

    http://tomsbigbox.com/pass-data-from-modal-view-back-to-parent-in-the-ios-sdk/

    【讨论】:

      【解决方案3】:

      解决方案 - 不要在 .h 中导入,在 .m 中导入以在 .h 中引用前向声明 @class/@protocol

      in .m - 有要关注的类别。 @interface 声音检测() @结束

      啊.h

      @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

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

      B.m

      #import "SoundDetect.h"
      @interface HelloScene()<SoundDetectDelegate>
      @end
      
      @implementation HelloScene
      
      -(void)update_from_sound: (id) node
      {
      
      }
      @end
      

      【讨论】:

        猜你喜欢
        • 2012-02-02
        • 2018-11-13
        • 1970-01-01
        • 1970-01-01
        • 2016-08-12
        • 2014-11-02
        • 2011-07-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多