2016-08-16 109 views
0

我試圖將按鈕移動到另一個按鈕的單擊位置。這是我的viewcontroller中的代碼。將按鈕移動到隨機位置(Swift)

@IBOutlet weak var samea: UIButton! 

@IBAction func yup(sender: UIButton) { 

    let buttonWidth = samea.frame.width 
    let buttonHeight = samea.frame.height 


    let viewWidth = samea.superview!.bounds.width 
    let viewHeight = samea.superview!.bounds.height 


    let xwidth = viewWidth - buttonWidth 
    let yheight = viewHeight - buttonHeight 


    let xoffset = CGFloat(arc4random_uniform(UInt32(xwidth))) 
    let yoffset = CGFloat(arc4random_uniform(UInt32(yheight))) 


    samea.center.x = xoffset + buttonWidth/2 
    samea.center.y = yoffset + buttonHeight/2 

    // Do any additional setup after loading the view, typically from a nib. 


} 
} 

我簡單地爲我想要移動的按鈕創建一個插座,然後爲移動它的按鈕創建一個插件。當我運行這個時,它在啓動時發生線程1:信號SIGABRT錯誤時崩潰。我該如何解決?

+0

你能給我們更多的錯誤輸出嗎?這還不夠。 –

+0

下面是錯誤的imgur鏈接:http://imgur.com/a/BFSfV – Ash

+0

看看左下方的框裏有什麼?我們需要看到這一點。這是我們將看到錯誤輸出的地方。 –

回答

0

所以你得到的錯誤是一個NSUnkownKeyExcpetion。我假設發生的事情是,當你將故事板中的視圖控制器連接到你的文件時,出現了一些混亂的情況。右鍵點擊你的按鈕,看看是否有任何兩個引用。如果這樣刪除一個。您也可能錯過了它認爲應該有的按鈕之一的插座。

+0

哦,傻了。忘記刪除幾個參考。感謝您的幫助,解決了它! – Ash