从自定义segue传递数据

时间:2016-02-16 08:05:35

标签: ios swift segue

我已经创建了一个自定义segue,我想知道在哪里放置func prepareForSegue来执行它。

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)
           }
      }
}

此代码执行segue但未将值传递给DestViewController - (它不执行func prepareForSegue

1 个答案:

答案 0 :(得分:1)

如果您的自定义segue仅针对一个视图编写,请执行以下操作:

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)
        }
    }
}

如果没有,请在ViewController之前将prepareForSegue函数移动到上一个视图控制器。