2016-10-09 138 views
1

進口AVFoundation 進口的UIKit無法覆蓋prepareForSegue功能

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功能,它拋出一個錯誤提超不具有覆蓋方法。

回答

4

在斯威夫特3的方法的簽名是

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

弄清楚自己是註釋掉整個方法並重新鍵入前幾個字符(prep)的一種方式。代碼完成將幫助你。