我的目的是清空2个数组中的值 - 强制[]&刚刚[],来自prepareforsegue的另一个类,当用户执行摇动手势时。下面是我的代码,然后是我遇到的错误。
springViewController1
var springNum : NSInteger = 0
class springViewController1 : UITableViewController, UITableViewDataSource {
var force = [Float]()
var stiffness = [Float] ()
}
springViewController2
class springViewController2 : UITableViewController , UITableViewDelegate, UITableViewDataSource {
var forceView2 = [Float]()
var stiffView2 = [Float]()
override func canBecomeFirstResponder() -> Bool {
return true
}
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
if(event.subtype == UIEventSubtype.MotionShake){
println("Shaken")
self.performSegueWithIdentifier("reset", sender: self)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if(segue.identifier == "reset") {
var svcSpringControl1 = segue.destinationViewController as springViewController1
springNum = 0
svcSpringControl1.force.removeAll(keepCapacity: true)
svcSpringControl1.stiffness.removeAll(keepCapacity: true)
}
}
}
代码在这里停止,即使我没有在代码中放置任何断点。我点击继续执行程序。
守则在这里停止。
答案 0 :(得分:1)
诀窍是创建递归函数以通过视图控制器堆栈:
func getSVC1FromVCStack(vc:UIViewController) -> springViewController1{
if(vc.isKindOfClass(springViewController1)){
return vc as springViewController1;
}
else if(vc.parentViewController == nil){
return springViewController1.alloc()
}
else {
return getSVC1FromVCStack(vc)
}
}
而不是
var svcSpringControl1 = segue.destinationViewController as springViewController1
应该是
var svcSpringControl1 = self.getSVC1FromVCStack(self.parentViewController!) as springViewController1
这是由展开segue引起的,所以SVC1不是目的地。另一项工作可能是参考SVC1。