Reputation: 1659
I am new to Xcode and swift but I have been able to narrow the error source down to this line of code. I have searched every where but could not find the solution. I'm currently using Xcode 8.1.
button.addTarget( self, action: #selector(handleRegister), for:.touchUpInside)
this is some of my code
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)
}
does anyone know what could be causing this Segmentation fault: 11 error.
Upvotes: 3
Views: 1008
Reputation: 407
To use self, you will have to use "lazy var" instead of "let" on the first line like below. Within the function, self refers to the function "loginRegisterButton", using "lazy var", self will refer to your controller
lazy var 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
}()
Upvotes: 0
Reputation: 1
Delete this line:
button.addTarget( self, action: #selector(handleRegister), for:.touchUpInside)
and write it in the viewDidLoad function
Upvotes: 0
Reputation: 487
IMHO the reason is that you use "self" in "button.addTarget( self, action: #selector(handleRegister), for:.touchUpInside)". To check that you could insert "nil" instead of "self". And further after you try inserting "nil", you could add "print(self)" to understand that "self" is referring not to "LoginController" as you intended.
Upvotes: 2