编辑以包含代码。
在 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/