PrepareforSegue函数永远不会运行,其他视图控制器中的变量是空白的

时间:2016-06-18 20:59:05

标签: ios swift segue

我试图将一些变量发送到另一个视图控制器,我以编程方式触发segue。由于某种原因,永远不会传递这些变量,并且永远不会运行print语句。当我尝试从下一个视图控制器检索变量时,变量只是nil和unassigned。我有以下函数实际触发segue,在我的视图控制器的底部是prepareforsegue:

func action(gestureRecognizer:UILongPressGestureRecognizer) {
    if (lpgr!.state == UIGestureRecognizerState.Began) {
        print("Began")
        let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        let documentsDirectory: AnyObject = paths[0]
        let format = NSDateFormatter()
        format.dateFormat="yyyy-MM-dd-HH-mm-ss"
        let dataPath = documentsDirectory.stringByAppendingPathComponent("/video-\(format.stringFromDate(NSDate())).mp4")
        let url = NSURL(fileURLWithPath: dataPath)

        videoOutput!.startRecordingToOutputFileURL(url, recordingDelegate: self)
        print("\(url)")

        NSUserDefaults.standardUserDefaults().setURL(url, forKey: "videoURL")
        NSUserDefaults.standardUserDefaults().synchronize()

    }

    if (lpgr!.state == UIGestureRecognizerState.Ended) {
        print("Ended")
        videoOutput!.stopRecording()
        url = NSUserDefaults.standardUserDefaults().URLForKey("videoURL")!
        videoData = NSData(contentsOfURL: url!)!



        print(videoData)

        presentViewController(PostViewController(), animated: true, completion: nil)



    }
}  

 override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "segueTest") {
        var svc = segue!.destinationViewController as! PostViewController;
        print("Variables saved")
        svc.VideoData = videoData
        svc.Url = url

    }
}

1 个答案:

答案 0 :(得分:2)

您的prepareForSegue永远不会被调用,因为您使用presentViewController()函数。它只是推动另一个视图控制器。 segue是Storyboard中两个视图控制器之间的链接,因此您可以在视图控制器之间的Storyboard中添加segue,将ID设置为segue并使用performSegueWithIdentifier(" mySegueID",sender:nil)。 / p>