Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在ViewController中获取委托方法以查看*recorder何时完成(不同类)_Ios_Objective C_Cocoa Touch_Delegates_Avaudiorecorder - Fatal编程技术网

Ios 如何在ViewController中获取委托方法以查看*recorder何时完成(不同类)

Ios 如何在ViewController中获取委托方法以查看*recorder何时完成(不同类),ios,objective-c,cocoa-touch,delegates,avaudiorecorder,Ios,Objective C,Cocoa Touch,Delegates,Avaudiorecorder,我有一个单身汉 @implementation RDTRecord @synthesize recorder; +(RDTRecord *)sharedRecorder { static RDTRecord* sharedRecorder; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedRecorder = [[RDTRecord alloc]init]; }); return share

我有一个单身汉

@implementation RDTRecord
@synthesize recorder;
+(RDTRecord *)sharedRecorder
{
static RDTRecord* sharedRecorder;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    sharedRecorder = [[RDTRecord alloc]init];
});
return sharedRecorder
}
以及一种方法:

- (void)doRecordAudio:(int)increment{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:nil];
... file location etc.
 recorder = [[AVAudioRecorder alloc]initWithURL:outputFileURL settings:settings               error:&error];
if (recorder) {
    [recorder prepareToRecord];

    [recorder recordForDuration:(NSTimeInterval) 3.0]
}
在类
RDTRecord

我使用以下命令从
RDTViewController
中的“录制”按钮调用此命令:

-(IBAction)recordButton:(UIButton *)sender

        //somenumber is not initialized yet so plug any int in here
        [[RDTRecord sharedRecorder] doRecordAudio:somenumber]; 
}
我想实现委托

-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)sharedRecorder successfully:(BOOL)flag {
...do some stuff here
}
在我的
RDTViewController
中,不在
RDTRecord

我的vc的界面是:

#import <UIKit/UIKit.h>
#import "RDTRecord.h"

@interface RDTViewController : UIViewController <AVAudioRecorderDelegate>;

@property (weak, nonatomic) IBOutlet UIButton *recordButton;

- (IBAction)recordButton:(UIButton *)sender;
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)sharedRecorder successfully:(BOOL)flag;
@end
问题是:如果
*录像机
在类
RDTRecord
中,我如何让我的
RDTViewController
中的委托方法
audiorecorderdifinishrecording
*录像机
完成时“查看”?
或者问同样问题的另一种方式是:如何让AVAudioRecorder*recorder对象“知道”如果代理在另一个类中,如
RDTViewController
,则要使用该代理,该代码会是什么样子?

RDTRecord
制作为录音机的代理,并使其实现
audiorecorderdinishrecording:successfully:
。但是,也给它一个property
@property(assign)id委托并且,当视图控制器想要触发录制时,将其自身设置为
RDTRecord
的代理

现在,当在
RDTRecord
中调用
audiorecorderdifinishrecording:successfully:
时,它可以将回调转发给其代理:

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)sharedRecorder successfully:(BOOL)flag
{
    [self.delegate audioRecorderDidFinishRecording:sharedRecorder successfully:flag];
}
(您还可以将委托添加为
doRecordAudio:
的参数,以及
nil
调用运行后的
委托
,具体取决于您的其他要求)


视图控制器在完成时(以及销毁之前)应将其自身作为代理删除。

我支持您直到“当您的视图控制器想要触发录制时,将其自身设置为
RDTRecord的代理。
”我会在
RDTViewController
中像
RDTRecord.delegate=self这样做吗
[RDTRecord setDelegate:self]
这两种方法在VC中都不起作用,因为VC中没有
委托
属性(在RDTRecord`)和setDelegate的已知类方法,这是因为您试图调用类,而不是实例:
[RDTRecord sharedRecorder].delegate=self
您就是那个人!我已经用了相当长的一段时间来反复讨论这个问题——似乎工作得很好!感谢您的参考:我制作了
RDTRecord
RDTViewController
,实现了
录音机的音频录制。我还添加了
recorder.delegate=self;`在
RDTRecord
中-使RDTRecord成为
AVAudioRecorder*recorder
的委托,然后触发
RDTRecord
中的委托方法,该方法调用
[self.delegate audiorecorderdifinishrecording:sharedRecorder successfully:flag]
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)sharedRecorder successfully:(BOOL)flag
{
    [self.delegate audioRecorderDidFinishRecording:sharedRecorder successfully:flag];
}