我已经创建了一个自定义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
)
答案 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
函数移动到上一个视图控制器。