簡體   English   中英

如何在ViewController中獲取委托方法以查看*記錄器何時完成(不同類)

[英]How can I get a delegate method in ViewController to see when *recorder is finished (different class)

我有一個單身人士

@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

.h for RDTRecord如下:

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import "RDTViewController.h"

@interface RDTRecord : NSObject {
    AVAudioRecorder *recorder;
}

@property (nonatomic,strong) AVAudioRecorder *recorder;

+(RDTRecord *)sharedRecorder;
- (void)doRecordAudio:(int)increment;
@end

問題是:如果*recorderRDTRecord類中,我RDTViewController才能在我的RDTViewController獲取委托方法audioRecorderDidFinishRecording以“看到” *recorder何時完成? 或者也許另一種問同樣問題的方法是:如果委托在另一個類中,如何讓AVAudioRecorder *記錄器對象“知道”使用委托 - 例如RDTViewController以及該代碼會是什么樣的?

RDTRecord成為錄音機的代表,讓它實現audioRecorderDidFinishRecording:successfully: . 但是,也給它一個屬性@property (assign) id <AVAudioRecorderDelegate> delegate; 並且,當您的視圖控制器想要觸發錄制時,它將自己設置為RDTRecord的委托。

現在,當audioRecorderDidFinishRecording:successfully:被稱為RDTRecord可以轉發回調其委托:

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)sharedRecorder successfully:(BOOL)flag
{
    [self.delegate audioRecorderDidFinishRecording:sharedRecorder successfully:flag];
}

(你也可以添加代理作為一個參數doRecordAudio:nildelegate回調已根據您的其他要求運行后)。

視圖控制器應該在完成后(在它被銷毀之前)作為委托刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM