maz
maz

Reputation: 397

PrepareforSegue function is never running, variables in additional view controller are blank

I am attempting to send some variables across to another view controller and I am triggering the segue programmatically. For some reason these variables are never being passed and the print statement is never being run. When I attempt to retrieve the variables form my next view controller the variables are simply nil and unassigned. I have this following function that actually triggers the segue, and at the bottom of my view controller the 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

    }
}

Upvotes: 2

Views: 54

Answers (1)

Remilia Da Costa Faro
Remilia Da Costa Faro

Reputation: 364

Your prepareForSegue is never called because you use presentViewController() function. It's simply push an other view controller. A segue is a link between two view controllers in Storyboard, so you can add a segue in Storyboard between view controllers, set an ID to the segue and use performSegueWithIdentifier("mySegueID", sender: nil) instead.

Upvotes: 2

Related Questions