swift - 无法覆盖 prepareForSegue 函数

标签 swift

<分区>

导入 AVFoundation 导入 UIKit

class RecordSoundsViewController: UIViewController, AVAudioRecorderDelegate{

    @IBOutlet weak var recordButton: UIButton!
    @IBOutlet weak var recordingLabel: UILabel!
    @IBOutlet weak var stopRecordingButton: UIButton!

    var audioRecorder: AVAudioRecorder!

    override func viewDidLoad() {
        super.viewDidLoad()
        stopRecordingButton.isEnabled = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func recordAudio(_ sender: AnyObject) {
        print("The record button was pressed!")
        recordingLabel.text = "Recording in Progress"
        stopRecordingButton.isEnabled = true
        recordButton.isEnabled = false

        let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)[0] as String
        let recordingName = "recordedVoice.wav"
        let pathArray = [dirPath, recordingName]
        let filePath = NSURL.fileURL(withPathComponents: pathArray)
        print(filePath)

        let session = AVAudioSession.sharedInstance()
        try! session.setCategory(AVAudioSessionCategoryPlayAndRecord)

        try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
        audioRecorder.delegate = self
        audioRecorder.isMeteringEnabled = true
        audioRecorder.prepareToRecord()
        audioRecorder.record()
    }

    @IBAction func stopRecording(_ sender: AnyObject) {
        print("Recording was stopped")
        recordingLabel.text = "Tap to Record"
        recordButton.isEnabled = true;
        stopRecordingButton.isEnabled = false
        audioRecorder.stop()
        let audioSession = AVAudioSession.sharedInstance()
        try! audioSession.setActive(false)
    }

    override func viewWillAppear(_ animated: Bool) {
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "stopRecording") {
            let playSoundsVC = segue.destination as! PlaySoundsViewController
            let recordedAudioURL = sender as! NSURL
            playSoundsVC.recordedAudioURL = recordedAudioURL
        }
    }

    func audioRecorderDidFinishRecording(recorder: AVAudioRecorder, successfully flag: Bool) {
        print("\n Av audio recorder has finished \n")
        if flag{
            self.performSegue(withIdentifier: "stopRecording", sender: audioRecorder.url)
        }else{
            print("Saving the audio file was unsucessfull")
        }
    }
}

当我尝试重写 prepareForSegue 函数时,它抛出一个错误,指出父类(super class)没有重写的方法。

最佳答案

在 Swift 3 中,方法的签名是

override func prepare(for segue: UIStoryboardSegue, sender: Any?)

了解自己的一种方法是注释掉整个方法并重新键入前几个字符 (prep)。代码完成将帮助您。

关于swift - 无法覆盖 prepareForSegue 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39946754/

相关文章:

ios - Swift NSNotificationCenter 在其他类中添加观察者功能

ios - Realm 、对象已被删除或失效

ios - URLSession 在 iOS 上使用自定义 CookieStorage?

ios - 填充两个 UITextField 的两个 UiDatePicker 之间的差异

ios - 该按钮只工作一次

ios - 使用 Swift 检查互联网连接

swift - 最大(_ :_:) and min(_:_:) in an Int extension in Swift

ios - 在录制的视频上合成基于帧的动画贴纸的最佳方法是什么?

iOS如何获取tableview单元格中整行的宽度

ios - Swift:在纺车概念中为 subview 启用 userInteractionEnabled