1

I've created a custom segue and I'm wondering where to put the func prepareForSegue for it to execute.

class CustomSegue: UIStoryboardSegue {

var a = 0
var b = 0

override func perform() {

    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let DestViewController : ViewController = segue.destinationViewController as! ViewController
        DestViewController.unlock = a + 10
        DestViewController.lock = b + 10
    }

    let source1 = sourceViewController as UIViewController
    let destination1 = destinationViewController as UIViewController

    let window = UIApplication.sharedApplication().keyWindow!

    destination1.view.alpha = 0.0
    window.insertSubview(destination1.view, belowSubview: source1.view)

    UIView.animateWithDuration(0.5, animations: { () -> Void in
        source1.view.alpha = 0.0
        destination1.view.alpha = 1.0
        }) { (finished) -> Void in
            source1.view.alpha = 0.0
            source1.presentViewController(destination1, animated: false, completion: nil)
           }
      }
}

This code performs the segue but does not pass the values to the DestViewController- (It does not execute the func prepareForSegue)

R.S
  • 253
  • 2
  • 7
  • 2
    `prepareForSegue` is a UIViewController function, not UIStoryboardSegue: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/prepareForSegue:sender: – Jörn Buitink Feb 16 '16 at 08:08

1 Answers1

1

If your custom segue written just for one view,Do like this:

class CustomSegue: UIStoryboardSegue {

    var a = 0
    var b = 0

    override func perform() {

        if let destViewController = destinationViewController as? ViewController {
            destViewController.unlock = a + 10
            destViewController.lock = b + 10
        }

        let source1 = sourceViewController as UIViewController
        let destination1 = destinationViewController as UIViewController

        let window = UIApplication.sharedApplication().keyWindow!

        destination1.view.alpha = 0.0
        window.insertSubview(destination1.view, belowSubview: source1.view)

        UIView.animateWithDuration(0.5, animations: { () -> Void in
            source1.view.alpha = 0.0
            destination1.view.alpha = 1.0
            }) { (finished) -> Void in
                source1.view.alpha = 0.0
                source1.presentViewController(destination1, animated: false, completion: nil)
        }
    }
}

If not,move prepareForSegue function to the previous view controller before ViewController.

Lumialxk
  • 6,239
  • 6
  • 24
  • 47