ios - 在 Objective C 中,A 类可以作为 B 类的委托(delegate),而 B 类可以作为 A 类的委托(delegate)吗?

标签 ios objective-c delegates

编辑以包含代码。

在 Objective C 中,是否可以让 A 类成为 B 类的委托(delegate),而 B 类作为 A 类的委托(delegate)?

我让第一个委托(delegate)(称之为 A 到 B)工作正常。然后我以另一种方式实现它,它找不到委托(delegate)方法,并出现经典警告: 协议(protocol)“SoundpaperViewControllerDelegate”中的方法“audioCueHasMovedDelegateMethod”未实现。

在我花时间上传代码之前,请告诉我这是否可行,我只是做错了什么,或者如果它一开始就不起作用,我需要使用另一种技术,例如NSNotification。我已完成谷歌搜索,但找不到此问题的地址。

如果这在理论上是合法的,那么我将上传我的代码。

谢谢。

在 Objective C 中,是否可以将方法 A 委托(delegate)给方法 B,而方法 B 是方法 A 的委托(delegate)?
我有第一个委托(delegate)(称之为 A 到 B)工作正常。然后我以另一种方式实现它,它找不到委托(delegate)方法,并出现经典警告: 协议(protocol)“SoundpaperViewControllerDelegate”中的方法“audioCueHasMovedDelegateMethod”未实现。

在我花时间上传代码之前,请告诉我这是否可行,我只是做错了什么,或者如果它一开始就不起作用,我需要使用另一种技术,例如NSNotification。我已经完成了谷歌搜索,但找不到已解决的问题。 谢谢。 这是 A 类: A类AudioOperations,这个调用了B类SoundpaperViewController中的一个方法,叫做setupAudioPlayerControls。这是因为 View Controller 是与屏幕对话的(使用 Storyboard)。虽然我想将音频播放器分成一个单独的类和单独的文件以保持清洁。 这很好用

音频操作.h

. . .
@class AudioOperations;
@class SoundPaperViewController;


@protocol AudioOperationsDelegate <NSObject>

- (void) setupAudioPlayerControls;  // THIS is called on Class B and works fine



@end

@interface  AudioOperations : NSObject

@property (nonatomic, weak) IBOutlet id <AudioOperationsDelegate> delegate;
. . .

@end

简化的 AudioOperations.mm

@interface AudioOperations () <AVAudioPlayerDelegate>

@end


- (void) audioPlayback  // simplified to show what is important
{
    NSLog(@"Entering audioPlayback");
    self.audioPlayer.delegate = self;

        NSLog(@"calling setupaudioplayercontrols from audioPlayback");
            [self.delegate setupAudioPlayerControls];  // delegated to soundpaperviewcontroller and works just fine


    NSLog(@"Exiting audioPlayback");
}


. . . (various other code)
- (void) audioCueHasMovedDelegateMethod  // THIS IS THE method that should be called from Class B but can’t be found (where the warning comes from)
{
    // User has released the cue while inside the control - update the player…
    NSLog(@"in delegate audioCueHasMoved");
    [self.audioPlayer setCurrentTime: self.delegate.audioCueSlider.value];

    if (self.audioPlayer.isPlaying == NO)
    {
        [self.audioPlayer play];
    }

    // … and go back to updating the control.

    self.audioTimer = [NSTimer scheduledTimerWithTimeInterval: 0.1
                                                       target: self
                                                     selector: @selector(updateAudioTime:)
                                                     userInfo: nil
                                                      repeats: YES];
}

这是 B 类(再次简化)

SoundpaperViewController.h
. . .

@class LabelProcessor;
@class AudioOperations;
@class SpotterGraphicsView;

@protocol SoundpaperViewControllerDelegate <NSObject>

- (void) audioCueHasMovedDelegateMethod;  // this is the method that should be called but isn’t found

@end


@interface SoundPaperViewController : UIViewController<QLPreviewControllerDataSource, QLPreviewControllerDelegate>

@property (nonatomic, weak) IBOutlet id <SoundpaperViewControllerDelegate> delegate;

. . .

@end


SoundpaperViewController.mm
. . . (bunch of #imports etc)
@interface SoundPaperViewController () <AVCaptureVideoDataOutputSampleBufferDelegate
    ,AVCaptureMetadataOutputObjectsDelegate
    ,LabelProcessorDelegate
    ,AudioOperationsDelegate
    ,SettingsObserver
    ,CEGuideArrowDelegate
    ,SoundpaperViewControllerDelegate  // NOT SURE THIS IS RIGHT, but I’ve tried it with and without it
    >
. . . (bunch of @properties, etc.)
@implementation SoundPaperViewController  // WARNING IS HERE that the method  audioCueHasMovedDelegateMethod  can’t be found


- (id) initWithCoder: (NSCoder*) inDecoder
{
    if ((self = [super initWithCoder: inDecoder]) != nil)
    {
        NSLog(@"new latestImageCond created NEW");
        self.latestImageCondition = [NSCondition new];
        NSLog(@"initWithCoder spvc thread: %@", [NSThread currentThread]);
    }

    self.delegate = self;  // NOTE this

    return self;
}


// This is the delegate method called from CLASS A (AudioOperations) and works just fine:
- (void) setupAudioPlayerControls
{

    NSLog(@"setupAudioPlayerControls");
    // hide the playbutton
    [self.playButton setEnabled:NO];
    [self.playButton setTintColor: [UIColor clearColor]];  // hides it

. . . etc.


}

据我所知,我在两个类中所做的一切都是相同的(除了一个异常(exception),我不确定是否要在界面中使用这个 SoundpaperViewControllerDelegate。

感谢您处理这些乱七八糟的代码。我确定我遗漏了一些明显的东西!

最佳答案

是的,这是可以做到的。确保每一个都对另一个有弱引用,这样您就不会以保留循环结束。

关于ios - 在 Objective C 中,A 类可以作为 B 类的委托(delegate),而 B 类可以作为 A 类的委托(delegate)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44187422/

相关文章:

ios - 如何在 SwiftUI View 中显示协议(protocol) View ?

C# 和委托(delegate)

c# - 在 C# 2.0 中使用委托(delegate)结果作为输入

objective-c - 返回位于本地堆栈上的 block

ios - 在聊天中分享图片,base64 问题

ios - 如何区分用户是用手指还是苹果铅笔点击屏幕?

objective-c - UIAlertView clickedButtonAtIndex 未调用

ios - swift 3 : Instance Member cannot be used on type “View Controller”

xcode - 显式将基础 SDK 设置为 3.2 以捕获编译器错误

ios - 保存/编辑重复日历事件的最佳方法