无法覆盖prepareForSegue函数[重复]

Posted

技术标签:

【中文标题】无法覆盖prepareForSegue函数[重复]【英文标题】:Cannot override prepareForSegue function [duplicate] 【发布时间】:2016-10-09 17:58:41 【问题描述】:

导入 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 函数时,它会抛出一个错误,指出超类没有被重写的方法。

【问题讨论】:

【参考方案1】:

在 Swift 3 中,方法的签名是

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

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

【讨论】:

以上是关于无法覆盖prepareForSegue函数[重复]的主要内容,如果未能解决你的问题,请参考以下文章

覆盖 prepareForSegue 函数时 IBAction 中的线程错误

无法使用 [NSIndexPath] 类型的索引为 [String] 类型的值下标 - prepareForSegue

如果我使用 prepareForSegue 传递数据,则无法嵌入导航控制器

prepareForSegue 不适用于自定义 segue (swift)

prepareForSegue 里面的函数不会执行

prepareforsegue 函数中的变量为空