2016-11-01 70 views
3

我是Xcode和swift的新手,但我已經能夠將錯誤源縮小到這行代碼。我搜索了每個地方,但找不到解決方案。我目前正在使用Xcode 8.1。Swift編譯器錯誤:命令由於信號失敗:分段錯誤:11

 button.addTarget(self, action: #selector(handleRegister), for:.touchUpInside) 

這是我的一些代碼

import UIKit 

class LoginController: UIViewController { 

let backGroundImageView: UIImageView = { 
    let imageView = UIImageView() 
    imageView.image = UIImage(named: "backgrd_image") 
    return imageView 
}() 

let inputsContainerView: UIView = { 
    let view = UIView() 
    view.backgroundColor = UIColor.white 
    view.translatesAutoresizingMaskIntoConstraints = false 
    view.layer.cornerRadius = 5 
    view.layer.masksToBounds = true 
    return view 

}() 

    let loginRegisterButton: UIButton = { 
    let button = UIButton(type: .system) 
    button.backgroundColor = UIColor(r: 80, g: 101, b: 161) 
    button.setTitle("Register", for: .normal) 
    button.setTitleColor(UIColor.white, for: .normal) 
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16) 
    button.translatesAutoresizingMaskIntoConstraints = false 

    button.addTarget(self, action: #selector(handleRegister), for:.touchUpInside) 
    return button 
}() 

// when register button is clicked, printout 123 
func handleRegister() { 
    print(123) 

} 

有誰知道這可能是導致此分段故障:11錯誤。

+0

分割故障是不是一個編譯器錯誤,這是一個運行時錯誤。 – Alexander

+1

不一定。隨着時間的推移,swift編譯器崩潰了很多,試圖編譯我的代碼。運行時崩潰在iOS中生成EXC_INV_OP,而不是分段錯誤。分段錯誤在MacOS中。 – BaseZen

+0

@ e.iluf您需要轉到「Build」日誌並粘貼對應於崩潰的日誌條目。這是Xcode 5,但它足夠接近:http://stackoverflow.com/questions/19014359/how-do-i-view-the-full-build-log-on-xcode5 – BaseZen

回答

2

恕我直言 原因是你在「button.addTarget(self,action:#selector(handleRegister),用於:.touchUpInside)」中使用「self」。檢查你是否可以插入「零」而不是「自我」。並且,當您嘗試插入「nil」後,您可以添加「print(self)」,以瞭解「self」並非如您所期望的那樣指的是「LoginController」。

+0

它的工作!..你是我的英雄人,我祝願所有美好的事物。謝謝! –

+0

哇。它效果很好。 – Neela

+0

不確定爲什麼它不是被接受的答案。 – Neela

0

刪除這一行:

button.addTarget(self, action: #selector(handleRegister), for:.touchUpInside) 

,並把它寫在viewDidLoad中功能

相關問題