简体   繁体   English

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

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

I have a singleton 我有一个单身人士

@implementation RDTRecord
@synthesize recorder;
+(RDTRecord *)sharedRecorder
{
static RDTRecord* sharedRecorder;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    sharedRecorder = [[RDTRecord alloc]init];
});
return sharedRecorder
}

and a method: 和方法:

- (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]
}

in a class RDTRecord RDTRecord

I call this from a "record" button in RDTViewController using the following: 我使用以下命令从RDTViewController的“记录”按钮调用它:

-(IBAction)recordButton:(UIButton *)sender

        //somenumber is not initialized yet so plug any int in here
        [[RDTRecord sharedRecorder] doRecordAudio:somenumber]; 
}

I want to implement the delegate... 我想实现委托......

-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)sharedRecorder successfully:(BOOL)flag {
...do some stuff here
}

in my RDTViewController not in RDTRecord 在我的RDTViewController 不在 RDTRecord

the interface for my vc is : 我的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 is as follows: .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

The question is: How can I get the delegate method audioRecorderDidFinishRecording in my RDTViewController to "see" when the *recorder is finished if the *recorder is in class RDTRecord ? 问题是:如果*recorderRDTRecord类中,我RDTViewController才能在我的RDTViewController获取委托方法audioRecorderDidFinishRecording以“看到” *recorder何时完成? Or perhaps another way to ask the same question would be: how do I let the AVAudioRecorder *recorder object "know" to use the delegate if the delegate is in another class - such as RDTViewController and what would that code look like? 或者也许另一种问同样问题的方法是:如果委托在另一个类中,如何让AVAudioRecorder *记录器对象“知道”使用委托 - 例如RDTViewController以及该代码会是什么样的?

Make RDTRecord the delegate of the audio recorder and have it implement audioRecorderDidFinishRecording:successfully: . RDTRecord成为录音机的代表,让它实现audioRecorderDidFinishRecording:successfully: . But, also give it a property @property (assign) id <AVAudioRecorderDelegate> delegate; 但是,也给它一个属性@property (assign) id <AVAudioRecorderDelegate> delegate; and, when your view controller wants to trigger a recording have it set itself as the delegate of RDTRecord . 并且,当您的视图控制器想要触发录制时,它将自己设置为RDTRecord的委托。

Now, when audioRecorderDidFinishRecording:successfully: is called in RDTRecord it can forward the callback to its delegate: 现在,当audioRecorderDidFinishRecording:successfully:被称为RDTRecord可以转发回调其委托:

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

(you could also add the delegate as a parameter of doRecordAudio: , and nil the delegate after the callback has run depending on your other requirements). (你也可以添加代理作为一个参数doRecordAudio:nildelegate回调已根据您的其他要求运行后)。

The view controller should remove itself as a delegate when it is done (and before it is destroyed). 视图控制器应该在完成后(在它被销毁之前)作为委托删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在iOS7的其他ViewController中使用NSURLSession委托方法吗? - Can I have a NSURLSession delegate method in a different ViewController in iOS7? 如何在同一个类中的awakeFromNib 方法中获取当前视图控制器? - How can I get the current viewcontroller in awakeFromNib method in the same class? 记录器委托方法未调用 - Recorder delegate method not called 当我仍然可以看到背景viewController时,如何呈现viewController - How to present viewController while I can still see the background viewController 我可以将委托定位到ViewController的“特定”实例以获取其数据吗? - Can I target a delegate to a “specific” instance of a ViewController, to get it's data? 使用故事板时如何从应用程序委托中获取可见的viewController? - How to get visible viewController from app delegate when using storyboard? 如何将两个委托放到一个视图控制器 - How can I put two delegate to one viewcontroller 我可以使用类方法作为委托回调吗 - Can I use a class method as a delegate callback 接收不同类的委托方法 - Receive delegate method in different class 我可以在ViewController的类中调用哪种方法来检查何时将其带到前台? - what method within a ViewController's class can I call to check when it has been brought to the foreground?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM