2015-04-07 64 views
0

我的意圖是當用戶執行一個搖動手勢時,從另一個類prepareforsegue中清空2個數組內的值 - force [] & stiffness []。下面是我的代碼,然後是我遇到的錯誤。Swift - Shake Motion做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) 

    } 
} 

} 

守則止於此,即使我沒有放置代碼內的任何斷點。我點擊繼續程序執行。 BreakPoint

代碼在這裏停止。 Error

+0

請繼續執行,直到您收到異常消息。 – Miknash

+0

它在這裏崩潰,不能再執行。代碼首先包含任何斷點。不知道爲什麼它會停在... –

+0

你試過添加異常斷點嗎? – Miknash

回答

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 

這引起放慢繼續,所以SVC1不是目的地。另一個解決方法可以參考SVC1。