在目标C中,类A可以是类B的委托,而类B是类A的委托?
编辑为包含代码。在目标C中,类A可以是类B的委托,而类B是类A的委托?
目标C中可能有类A是类B的委托,而类B是类A的委托吗?
我有第一个委托(称之为A到B)工作正常。然后我以其他方式实现它,它无法找到委托方法,并带有经典警告: 协议'SoundpaperViewControllerDelegate'中的方法'audioCueHasMovedDelegateMethod'未实现。
在我花时间上传代码之前,请让我知道这是否可能,我只是做错了什么,或者如果它不能工作,我需要使用另一种技术作为NSNotification。我做了我的谷歌搜索,但无法找到这个问题。
如果这在理论上是合法的,那么我会上传我的代码。
谢谢。
在Objective C中,有可能让方法A委托给方法B,而方法B是方法A的延迟?
我有第一个委托(称为A到B)工作正常。然后我以其他方式实现它,它无法找到委托方法,并带有经典警告: 协议'SoundpaperViewControllerDelegate'中的方法'audioCueHasMovedDelegateMethod'未实现。
在我花时间上传代码之前,请让我知道这是否可能,我只是做错了什么,或者如果它不能工作,我需要使用另一种技术作为NSNotification。我做了我的谷歌搜索,但无法找到这个问题。 谢谢。 这是A类: A类AudioOperations,它调用B类SoundpaperViewController中的一个方法,名为setupAudioPlayerControls。这是因为视图控制器是与屏幕交谈的内容(使用Storyboard)。虽然我想将音频播放器分成单独的课程和单独的文件以保持清洁。 这只是正常
AudioOperations.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];
}
THIS IS 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.
}
尽可能接近我可以告诉我在两个班里都做了一切一样的事情我不确定是否在界面中使用这个SoundpaperViewControllerDelegate。
感谢您通过这一混乱的代码工作。我相信我错过了一些明显的东西!
在朋友的帮助下,我能够得到这个工作。对于那些可能有类似问题的人来说,这是一个让.h文件正确设置的问题:
在SoundpaperViewController中。H:
@class LabelProcessor;
@class AudioOperations;
@class SoundPaperViewController;
@interface SoundPaperViewController : UIViewController < QLPreviewControllerDataSource, QLPreviewControllerDelegate> // note no audioOperationsDelegate in this line
- (void) setupAudioPlayerControls;
@property (nonatomic, strong) AudioOperations * SPVdelegate;
在AudioOperations类:
@class AudioOperations;
@class SoundPaperViewController;
@interface AudioOperations : NSObject // note no SoundpaperViewControllerDelegate here
@property (nonatomic, weak) SoundPaperViewController * audioDelegate;
- (void) audioCueHasMovedDelegateMethod;
而在Soundpaper类.m文件:
_myAudio = [[AudioOperations alloc] init];
_myAudio.audioDelegate = self;
self.SPVdelegate = _myAudio;
这是启发了我,我没有需要包括@interface中的委托(例如@interface SoundpaperViewController)摆脱这种情况并简单地声明@property就是所需要的。
是的,这可以做到。确保每个人都有一个对另一个的弱引用,以便最终不会留下保留周期。
只有一个需要是一个弱引用。如果他们都很弱,那么其他东西需要拥有_both_。 –
是的,当然可以,“委托”不是语言功能,它只是一种设计模式,您可以按照自己想要的方式创建它们,包括双向。 – luk2302
这是绝对可能的。但是让它变弱,因为你知道。其他保留周期将发生 –
谢谢,这就是我的想法。我正在编写代码示例,谢谢。 – user938797