Action UIbutton programmatically Swift 3












0














I don't really understand what's wrong with my code because I did exactly the same thing as what I did on another controller and now it works. When I click on the button, nothing happens, the function is not called.



Here is my code :



class ViewControllerHome: UIViewController, UITextViewDelegate {


var btnFR : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.setImage(#imageLiteral(resourceName: "Bouton FR"), for: UIControlState.normal)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(ViewControllerHome.pressBtnFR), for: UIControlEvents.touchUpInside)
return btn
}()

func pressBtnFR() {
saveUserLanguage(language: "FR")
print(ViewControllerHome.getloadLanguage())
}


Thanks in advance for your help.



As you told me, I later added the target. But it doesn't work any better. Here is my code in its entirety.



class ViewControllerHome: UIViewController, UITextViewDelegate {

var currentScreen : Int8?

var imgVwBackground : UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "Background")
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()

var imgVwLogo : UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "LOGO MJM")
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()

var imgVwAppName : UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "MAROLLES")
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()

var btnFR : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.setImage(#imageLiteral(resourceName: "Bouton FR"), for: UIControlState.normal)
btn.translatesAutoresizingMaskIntoConstraints = false
//btn.addTarget(self, action: #selector(ViewControllerHome.pressBtnFR), for: UIControlEvents.touchUpInside)
return btn
}()

var btnNL : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.setImage(#imageLiteral(resourceName: "Bouton NL"), for: UIControlState.normal)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(ViewControllerHome.pressBtnNL), for: UIControlEvents.touchUpInside)
return btn
}()


static let USER_LANGUAGE_KEY : String = "USER_LANGUAGE"
static let USER_MASCOT_GENDER_KEY : String = "MASCOT_GENDER"
let MASCOT_BOY_NAME : String = "Maxime"
let MASCOT_GIRL_NAME : String = "Julie"

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

screen1()
self.btnFR.addTarget(self, action: #selector(pressBtnFR), for:.touchUpInside)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func unwindfromOption(_ segue: UIStoryboardSegue) {

}

//-----------------screen 1 : language choice--------------------

let french : String = "Français"
let dutch : String = "Nederlands"

func pressBtnFR() {
saveUserLanguage(language: "FR")
print(ViewControllerHome.getloadLanguage())
}

func pressBtnNL() {
saveUserLanguage(language: "NL")
print(ViewControllerHome.getloadLanguage())
}

func saveUserLanguage (language : String) {
let userDefaultsTemp = UserDefaults.standard
userDefaultsTemp.set(language, forKey: ViewControllerHome.USER_LANGUAGE_KEY)
}

static func getloadLanguage () -> String {
var language : String = ""
let userDefaultsTemp = UserDefaults.standard
let languageTemp: String? = userDefaultsTemp.object(forKey: ViewControllerHome.USER_LANGUAGE_KEY) as? String
if languageTemp != nil {
language = languageTemp!
}
return language
}
//----------Visual configuration---------------
func screen1 () {
let fibo236 : CGFloat = 0.236
let fibo382 : CGFloat = 0.382
let fibo5 : CGFloat = 0.5
let fibo618 : CGFloat = 0.618
let fibo786 : CGFloat = 0.786

currentScreen = 1

view.addSubview(imgVwBackground)
imgVwBackground.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imgVwBackground.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
imgVwBackground.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
imgVwBackground.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true

imgVwBackground.addSubview(imgVwAppName)
imgVwAppName.centerXAnchor.constraint(equalTo: imgVwBackground.centerXAnchor).isActive = true
imgVwAppName.centerYAnchor.constraint(equalTo: imgVwBackground.centerYAnchor, constant: 20).isActive = true
imgVwAppName.widthAnchor.constraint(equalTo: imgVwBackground.widthAnchor, multiplier: fibo5).isActive = true
imgVwAppName.heightAnchor.constraint(equalTo: imgVwAppName.widthAnchor, multiplier:fibo5).isActive = true

imgVwBackground.addSubview(imgVwLogo)
imgVwLogo.bottomAnchor.constraint(equalTo: imgVwAppName.topAnchor, constant: -20).isActive = true
imgVwLogo.centerXAnchor.constraint(equalTo: imgVwBackground.centerXAnchor, constant: 0).isActive = true
imgVwLogo.widthAnchor.constraint(equalTo: imgVwAppName.widthAnchor, multiplier: fibo382).isActive = true
imgVwLogo.heightAnchor.constraint(equalTo: imgVwLogo.widthAnchor, multiplier: 1.618).isActive = true

imgVwBackground.addSubview(btnFR)
btnFR.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: fibo236/2.5).isActive = true
btnFR.widthAnchor.constraint(equalTo: btnFR.heightAnchor, multiplier: 1).isActive = true
btnFR.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -80).isActive = true
btnFR.rightAnchor.constraint(equalTo: view.centerXAnchor, constant: -7.5).isActive = true

imgVwBackground.addSubview(btnNL)
btnNL.heightAnchor.constraint(equalTo: btnFR.heightAnchor).isActive = true
btnNL.widthAnchor.constraint(equalTo: btnFR.widthAnchor).isActive = true
btnNL.centerYAnchor.constraint(equalTo: btnFR.centerYAnchor).isActive = true
btnNL.leftAnchor.constraint(equalTo: view.centerXAnchor, constant: 7.5).isActive = true
}









share|improve this question
























  • I'm assuming that you have used btnFR when you added it as a subview to the controllers view
    – MadProgrammer
    Nov 21 at 3:04










  • So where to you reference your btnFR variable? You need to do something with that button object.
    – Duncan C
    Nov 21 at 3:10










  • This code will never work, you need to add the target later on during the viewdidload and please don’t use Viewcontroller.pressBtnFr just use the name of the method. Secondly you need to add the button as a subview and finally set the frame and/or and some anchors or constraints
    – OverD
    Nov 21 at 3:37










  • Yes I don't paste this part of the code : I call the function screen1() on viewDidLoad and I added the button as a subview of one UIimageView wich is a subview of the basic view of the controller. view.addSubview(imgVwBackground) imgVwBackground.addSubview(btnFR)
    – Jodonné Yazigi
    Nov 21 at 14:09


















0














I don't really understand what's wrong with my code because I did exactly the same thing as what I did on another controller and now it works. When I click on the button, nothing happens, the function is not called.



Here is my code :



class ViewControllerHome: UIViewController, UITextViewDelegate {


var btnFR : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.setImage(#imageLiteral(resourceName: "Bouton FR"), for: UIControlState.normal)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(ViewControllerHome.pressBtnFR), for: UIControlEvents.touchUpInside)
return btn
}()

func pressBtnFR() {
saveUserLanguage(language: "FR")
print(ViewControllerHome.getloadLanguage())
}


Thanks in advance for your help.



As you told me, I later added the target. But it doesn't work any better. Here is my code in its entirety.



class ViewControllerHome: UIViewController, UITextViewDelegate {

var currentScreen : Int8?

var imgVwBackground : UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "Background")
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()

var imgVwLogo : UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "LOGO MJM")
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()

var imgVwAppName : UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "MAROLLES")
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()

var btnFR : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.setImage(#imageLiteral(resourceName: "Bouton FR"), for: UIControlState.normal)
btn.translatesAutoresizingMaskIntoConstraints = false
//btn.addTarget(self, action: #selector(ViewControllerHome.pressBtnFR), for: UIControlEvents.touchUpInside)
return btn
}()

var btnNL : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.setImage(#imageLiteral(resourceName: "Bouton NL"), for: UIControlState.normal)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(ViewControllerHome.pressBtnNL), for: UIControlEvents.touchUpInside)
return btn
}()


static let USER_LANGUAGE_KEY : String = "USER_LANGUAGE"
static let USER_MASCOT_GENDER_KEY : String = "MASCOT_GENDER"
let MASCOT_BOY_NAME : String = "Maxime"
let MASCOT_GIRL_NAME : String = "Julie"

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

screen1()
self.btnFR.addTarget(self, action: #selector(pressBtnFR), for:.touchUpInside)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func unwindfromOption(_ segue: UIStoryboardSegue) {

}

//-----------------screen 1 : language choice--------------------

let french : String = "Français"
let dutch : String = "Nederlands"

func pressBtnFR() {
saveUserLanguage(language: "FR")
print(ViewControllerHome.getloadLanguage())
}

func pressBtnNL() {
saveUserLanguage(language: "NL")
print(ViewControllerHome.getloadLanguage())
}

func saveUserLanguage (language : String) {
let userDefaultsTemp = UserDefaults.standard
userDefaultsTemp.set(language, forKey: ViewControllerHome.USER_LANGUAGE_KEY)
}

static func getloadLanguage () -> String {
var language : String = ""
let userDefaultsTemp = UserDefaults.standard
let languageTemp: String? = userDefaultsTemp.object(forKey: ViewControllerHome.USER_LANGUAGE_KEY) as? String
if languageTemp != nil {
language = languageTemp!
}
return language
}
//----------Visual configuration---------------
func screen1 () {
let fibo236 : CGFloat = 0.236
let fibo382 : CGFloat = 0.382
let fibo5 : CGFloat = 0.5
let fibo618 : CGFloat = 0.618
let fibo786 : CGFloat = 0.786

currentScreen = 1

view.addSubview(imgVwBackground)
imgVwBackground.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imgVwBackground.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
imgVwBackground.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
imgVwBackground.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true

imgVwBackground.addSubview(imgVwAppName)
imgVwAppName.centerXAnchor.constraint(equalTo: imgVwBackground.centerXAnchor).isActive = true
imgVwAppName.centerYAnchor.constraint(equalTo: imgVwBackground.centerYAnchor, constant: 20).isActive = true
imgVwAppName.widthAnchor.constraint(equalTo: imgVwBackground.widthAnchor, multiplier: fibo5).isActive = true
imgVwAppName.heightAnchor.constraint(equalTo: imgVwAppName.widthAnchor, multiplier:fibo5).isActive = true

imgVwBackground.addSubview(imgVwLogo)
imgVwLogo.bottomAnchor.constraint(equalTo: imgVwAppName.topAnchor, constant: -20).isActive = true
imgVwLogo.centerXAnchor.constraint(equalTo: imgVwBackground.centerXAnchor, constant: 0).isActive = true
imgVwLogo.widthAnchor.constraint(equalTo: imgVwAppName.widthAnchor, multiplier: fibo382).isActive = true
imgVwLogo.heightAnchor.constraint(equalTo: imgVwLogo.widthAnchor, multiplier: 1.618).isActive = true

imgVwBackground.addSubview(btnFR)
btnFR.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: fibo236/2.5).isActive = true
btnFR.widthAnchor.constraint(equalTo: btnFR.heightAnchor, multiplier: 1).isActive = true
btnFR.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -80).isActive = true
btnFR.rightAnchor.constraint(equalTo: view.centerXAnchor, constant: -7.5).isActive = true

imgVwBackground.addSubview(btnNL)
btnNL.heightAnchor.constraint(equalTo: btnFR.heightAnchor).isActive = true
btnNL.widthAnchor.constraint(equalTo: btnFR.widthAnchor).isActive = true
btnNL.centerYAnchor.constraint(equalTo: btnFR.centerYAnchor).isActive = true
btnNL.leftAnchor.constraint(equalTo: view.centerXAnchor, constant: 7.5).isActive = true
}









share|improve this question
























  • I'm assuming that you have used btnFR when you added it as a subview to the controllers view
    – MadProgrammer
    Nov 21 at 3:04










  • So where to you reference your btnFR variable? You need to do something with that button object.
    – Duncan C
    Nov 21 at 3:10










  • This code will never work, you need to add the target later on during the viewdidload and please don’t use Viewcontroller.pressBtnFr just use the name of the method. Secondly you need to add the button as a subview and finally set the frame and/or and some anchors or constraints
    – OverD
    Nov 21 at 3:37










  • Yes I don't paste this part of the code : I call the function screen1() on viewDidLoad and I added the button as a subview of one UIimageView wich is a subview of the basic view of the controller. view.addSubview(imgVwBackground) imgVwBackground.addSubview(btnFR)
    – Jodonné Yazigi
    Nov 21 at 14:09
















0












0








0







I don't really understand what's wrong with my code because I did exactly the same thing as what I did on another controller and now it works. When I click on the button, nothing happens, the function is not called.



Here is my code :



class ViewControllerHome: UIViewController, UITextViewDelegate {


var btnFR : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.setImage(#imageLiteral(resourceName: "Bouton FR"), for: UIControlState.normal)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(ViewControllerHome.pressBtnFR), for: UIControlEvents.touchUpInside)
return btn
}()

func pressBtnFR() {
saveUserLanguage(language: "FR")
print(ViewControllerHome.getloadLanguage())
}


Thanks in advance for your help.



As you told me, I later added the target. But it doesn't work any better. Here is my code in its entirety.



class ViewControllerHome: UIViewController, UITextViewDelegate {

var currentScreen : Int8?

var imgVwBackground : UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "Background")
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()

var imgVwLogo : UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "LOGO MJM")
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()

var imgVwAppName : UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "MAROLLES")
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()

var btnFR : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.setImage(#imageLiteral(resourceName: "Bouton FR"), for: UIControlState.normal)
btn.translatesAutoresizingMaskIntoConstraints = false
//btn.addTarget(self, action: #selector(ViewControllerHome.pressBtnFR), for: UIControlEvents.touchUpInside)
return btn
}()

var btnNL : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.setImage(#imageLiteral(resourceName: "Bouton NL"), for: UIControlState.normal)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(ViewControllerHome.pressBtnNL), for: UIControlEvents.touchUpInside)
return btn
}()


static let USER_LANGUAGE_KEY : String = "USER_LANGUAGE"
static let USER_MASCOT_GENDER_KEY : String = "MASCOT_GENDER"
let MASCOT_BOY_NAME : String = "Maxime"
let MASCOT_GIRL_NAME : String = "Julie"

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

screen1()
self.btnFR.addTarget(self, action: #selector(pressBtnFR), for:.touchUpInside)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func unwindfromOption(_ segue: UIStoryboardSegue) {

}

//-----------------screen 1 : language choice--------------------

let french : String = "Français"
let dutch : String = "Nederlands"

func pressBtnFR() {
saveUserLanguage(language: "FR")
print(ViewControllerHome.getloadLanguage())
}

func pressBtnNL() {
saveUserLanguage(language: "NL")
print(ViewControllerHome.getloadLanguage())
}

func saveUserLanguage (language : String) {
let userDefaultsTemp = UserDefaults.standard
userDefaultsTemp.set(language, forKey: ViewControllerHome.USER_LANGUAGE_KEY)
}

static func getloadLanguage () -> String {
var language : String = ""
let userDefaultsTemp = UserDefaults.standard
let languageTemp: String? = userDefaultsTemp.object(forKey: ViewControllerHome.USER_LANGUAGE_KEY) as? String
if languageTemp != nil {
language = languageTemp!
}
return language
}
//----------Visual configuration---------------
func screen1 () {
let fibo236 : CGFloat = 0.236
let fibo382 : CGFloat = 0.382
let fibo5 : CGFloat = 0.5
let fibo618 : CGFloat = 0.618
let fibo786 : CGFloat = 0.786

currentScreen = 1

view.addSubview(imgVwBackground)
imgVwBackground.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imgVwBackground.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
imgVwBackground.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
imgVwBackground.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true

imgVwBackground.addSubview(imgVwAppName)
imgVwAppName.centerXAnchor.constraint(equalTo: imgVwBackground.centerXAnchor).isActive = true
imgVwAppName.centerYAnchor.constraint(equalTo: imgVwBackground.centerYAnchor, constant: 20).isActive = true
imgVwAppName.widthAnchor.constraint(equalTo: imgVwBackground.widthAnchor, multiplier: fibo5).isActive = true
imgVwAppName.heightAnchor.constraint(equalTo: imgVwAppName.widthAnchor, multiplier:fibo5).isActive = true

imgVwBackground.addSubview(imgVwLogo)
imgVwLogo.bottomAnchor.constraint(equalTo: imgVwAppName.topAnchor, constant: -20).isActive = true
imgVwLogo.centerXAnchor.constraint(equalTo: imgVwBackground.centerXAnchor, constant: 0).isActive = true
imgVwLogo.widthAnchor.constraint(equalTo: imgVwAppName.widthAnchor, multiplier: fibo382).isActive = true
imgVwLogo.heightAnchor.constraint(equalTo: imgVwLogo.widthAnchor, multiplier: 1.618).isActive = true

imgVwBackground.addSubview(btnFR)
btnFR.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: fibo236/2.5).isActive = true
btnFR.widthAnchor.constraint(equalTo: btnFR.heightAnchor, multiplier: 1).isActive = true
btnFR.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -80).isActive = true
btnFR.rightAnchor.constraint(equalTo: view.centerXAnchor, constant: -7.5).isActive = true

imgVwBackground.addSubview(btnNL)
btnNL.heightAnchor.constraint(equalTo: btnFR.heightAnchor).isActive = true
btnNL.widthAnchor.constraint(equalTo: btnFR.widthAnchor).isActive = true
btnNL.centerYAnchor.constraint(equalTo: btnFR.centerYAnchor).isActive = true
btnNL.leftAnchor.constraint(equalTo: view.centerXAnchor, constant: 7.5).isActive = true
}









share|improve this question















I don't really understand what's wrong with my code because I did exactly the same thing as what I did on another controller and now it works. When I click on the button, nothing happens, the function is not called.



Here is my code :



class ViewControllerHome: UIViewController, UITextViewDelegate {


var btnFR : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.setImage(#imageLiteral(resourceName: "Bouton FR"), for: UIControlState.normal)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(ViewControllerHome.pressBtnFR), for: UIControlEvents.touchUpInside)
return btn
}()

func pressBtnFR() {
saveUserLanguage(language: "FR")
print(ViewControllerHome.getloadLanguage())
}


Thanks in advance for your help.



As you told me, I later added the target. But it doesn't work any better. Here is my code in its entirety.



class ViewControllerHome: UIViewController, UITextViewDelegate {

var currentScreen : Int8?

var imgVwBackground : UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "Background")
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()

var imgVwLogo : UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "LOGO MJM")
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()

var imgVwAppName : UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "MAROLLES")
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()

var btnFR : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.setImage(#imageLiteral(resourceName: "Bouton FR"), for: UIControlState.normal)
btn.translatesAutoresizingMaskIntoConstraints = false
//btn.addTarget(self, action: #selector(ViewControllerHome.pressBtnFR), for: UIControlEvents.touchUpInside)
return btn
}()

var btnNL : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.setImage(#imageLiteral(resourceName: "Bouton NL"), for: UIControlState.normal)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(ViewControllerHome.pressBtnNL), for: UIControlEvents.touchUpInside)
return btn
}()


static let USER_LANGUAGE_KEY : String = "USER_LANGUAGE"
static let USER_MASCOT_GENDER_KEY : String = "MASCOT_GENDER"
let MASCOT_BOY_NAME : String = "Maxime"
let MASCOT_GIRL_NAME : String = "Julie"

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

screen1()
self.btnFR.addTarget(self, action: #selector(pressBtnFR), for:.touchUpInside)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func unwindfromOption(_ segue: UIStoryboardSegue) {

}

//-----------------screen 1 : language choice--------------------

let french : String = "Français"
let dutch : String = "Nederlands"

func pressBtnFR() {
saveUserLanguage(language: "FR")
print(ViewControllerHome.getloadLanguage())
}

func pressBtnNL() {
saveUserLanguage(language: "NL")
print(ViewControllerHome.getloadLanguage())
}

func saveUserLanguage (language : String) {
let userDefaultsTemp = UserDefaults.standard
userDefaultsTemp.set(language, forKey: ViewControllerHome.USER_LANGUAGE_KEY)
}

static func getloadLanguage () -> String {
var language : String = ""
let userDefaultsTemp = UserDefaults.standard
let languageTemp: String? = userDefaultsTemp.object(forKey: ViewControllerHome.USER_LANGUAGE_KEY) as? String
if languageTemp != nil {
language = languageTemp!
}
return language
}
//----------Visual configuration---------------
func screen1 () {
let fibo236 : CGFloat = 0.236
let fibo382 : CGFloat = 0.382
let fibo5 : CGFloat = 0.5
let fibo618 : CGFloat = 0.618
let fibo786 : CGFloat = 0.786

currentScreen = 1

view.addSubview(imgVwBackground)
imgVwBackground.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imgVwBackground.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
imgVwBackground.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
imgVwBackground.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true

imgVwBackground.addSubview(imgVwAppName)
imgVwAppName.centerXAnchor.constraint(equalTo: imgVwBackground.centerXAnchor).isActive = true
imgVwAppName.centerYAnchor.constraint(equalTo: imgVwBackground.centerYAnchor, constant: 20).isActive = true
imgVwAppName.widthAnchor.constraint(equalTo: imgVwBackground.widthAnchor, multiplier: fibo5).isActive = true
imgVwAppName.heightAnchor.constraint(equalTo: imgVwAppName.widthAnchor, multiplier:fibo5).isActive = true

imgVwBackground.addSubview(imgVwLogo)
imgVwLogo.bottomAnchor.constraint(equalTo: imgVwAppName.topAnchor, constant: -20).isActive = true
imgVwLogo.centerXAnchor.constraint(equalTo: imgVwBackground.centerXAnchor, constant: 0).isActive = true
imgVwLogo.widthAnchor.constraint(equalTo: imgVwAppName.widthAnchor, multiplier: fibo382).isActive = true
imgVwLogo.heightAnchor.constraint(equalTo: imgVwLogo.widthAnchor, multiplier: 1.618).isActive = true

imgVwBackground.addSubview(btnFR)
btnFR.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: fibo236/2.5).isActive = true
btnFR.widthAnchor.constraint(equalTo: btnFR.heightAnchor, multiplier: 1).isActive = true
btnFR.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -80).isActive = true
btnFR.rightAnchor.constraint(equalTo: view.centerXAnchor, constant: -7.5).isActive = true

imgVwBackground.addSubview(btnNL)
btnNL.heightAnchor.constraint(equalTo: btnFR.heightAnchor).isActive = true
btnNL.widthAnchor.constraint(equalTo: btnFR.widthAnchor).isActive = true
btnNL.centerYAnchor.constraint(equalTo: btnFR.centerYAnchor).isActive = true
btnNL.leftAnchor.constraint(equalTo: view.centerXAnchor, constant: 7.5).isActive = true
}






swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 at 5:35









Cœur

17.3k9102144




17.3k9102144










asked Nov 21 at 3:00









Jodonné Yazigi

54




54












  • I'm assuming that you have used btnFR when you added it as a subview to the controllers view
    – MadProgrammer
    Nov 21 at 3:04










  • So where to you reference your btnFR variable? You need to do something with that button object.
    – Duncan C
    Nov 21 at 3:10










  • This code will never work, you need to add the target later on during the viewdidload and please don’t use Viewcontroller.pressBtnFr just use the name of the method. Secondly you need to add the button as a subview and finally set the frame and/or and some anchors or constraints
    – OverD
    Nov 21 at 3:37










  • Yes I don't paste this part of the code : I call the function screen1() on viewDidLoad and I added the button as a subview of one UIimageView wich is a subview of the basic view of the controller. view.addSubview(imgVwBackground) imgVwBackground.addSubview(btnFR)
    – Jodonné Yazigi
    Nov 21 at 14:09




















  • I'm assuming that you have used btnFR when you added it as a subview to the controllers view
    – MadProgrammer
    Nov 21 at 3:04










  • So where to you reference your btnFR variable? You need to do something with that button object.
    – Duncan C
    Nov 21 at 3:10










  • This code will never work, you need to add the target later on during the viewdidload and please don’t use Viewcontroller.pressBtnFr just use the name of the method. Secondly you need to add the button as a subview and finally set the frame and/or and some anchors or constraints
    – OverD
    Nov 21 at 3:37










  • Yes I don't paste this part of the code : I call the function screen1() on viewDidLoad and I added the button as a subview of one UIimageView wich is a subview of the basic view of the controller. view.addSubview(imgVwBackground) imgVwBackground.addSubview(btnFR)
    – Jodonné Yazigi
    Nov 21 at 14:09


















I'm assuming that you have used btnFR when you added it as a subview to the controllers view
– MadProgrammer
Nov 21 at 3:04




I'm assuming that you have used btnFR when you added it as a subview to the controllers view
– MadProgrammer
Nov 21 at 3:04












So where to you reference your btnFR variable? You need to do something with that button object.
– Duncan C
Nov 21 at 3:10




So where to you reference your btnFR variable? You need to do something with that button object.
– Duncan C
Nov 21 at 3:10












This code will never work, you need to add the target later on during the viewdidload and please don’t use Viewcontroller.pressBtnFr just use the name of the method. Secondly you need to add the button as a subview and finally set the frame and/or and some anchors or constraints
– OverD
Nov 21 at 3:37




This code will never work, you need to add the target later on during the viewdidload and please don’t use Viewcontroller.pressBtnFr just use the name of the method. Secondly you need to add the button as a subview and finally set the frame and/or and some anchors or constraints
– OverD
Nov 21 at 3:37












Yes I don't paste this part of the code : I call the function screen1() on viewDidLoad and I added the button as a subview of one UIimageView wich is a subview of the basic view of the controller. view.addSubview(imgVwBackground) imgVwBackground.addSubview(btnFR)
– Jodonné Yazigi
Nov 21 at 14:09






Yes I don't paste this part of the code : I call the function screen1() on viewDidLoad and I added the button as a subview of one UIimageView wich is a subview of the basic view of the controller. view.addSubview(imgVwBackground) imgVwBackground.addSubview(btnFR)
– Jodonné Yazigi
Nov 21 at 14:09














1 Answer
1






active

oldest

votes


















0














class ViewController:UIViewController{

override func viewDidLoad() {

super.viewDidLoad()


self.view.addSubview(btnFR)

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .width , relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 0.9, constant: 0.0))

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 0.7, constant: 0.0))


self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0.0))

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0.0))

}


var btnFR : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.backgroundColor = UIColor.red
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(ViewController.pressBtnFR), for: UIControlEvents.touchUpInside)
return btn
}()

func pressBtnFR() {
print("Test")
}
}


Add the button to the Self view. It is Missing






share|improve this answer























  • I added it, I forgot to paste it on my topic
    – Jodonné Yazigi
    Nov 21 at 14:13











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404686%2faction-uibutton-programmatically-swift-3%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














class ViewController:UIViewController{

override func viewDidLoad() {

super.viewDidLoad()


self.view.addSubview(btnFR)

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .width , relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 0.9, constant: 0.0))

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 0.7, constant: 0.0))


self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0.0))

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0.0))

}


var btnFR : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.backgroundColor = UIColor.red
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(ViewController.pressBtnFR), for: UIControlEvents.touchUpInside)
return btn
}()

func pressBtnFR() {
print("Test")
}
}


Add the button to the Self view. It is Missing






share|improve this answer























  • I added it, I forgot to paste it on my topic
    – Jodonné Yazigi
    Nov 21 at 14:13
















0














class ViewController:UIViewController{

override func viewDidLoad() {

super.viewDidLoad()


self.view.addSubview(btnFR)

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .width , relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 0.9, constant: 0.0))

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 0.7, constant: 0.0))


self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0.0))

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0.0))

}


var btnFR : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.backgroundColor = UIColor.red
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(ViewController.pressBtnFR), for: UIControlEvents.touchUpInside)
return btn
}()

func pressBtnFR() {
print("Test")
}
}


Add the button to the Self view. It is Missing






share|improve this answer























  • I added it, I forgot to paste it on my topic
    – Jodonné Yazigi
    Nov 21 at 14:13














0












0








0






class ViewController:UIViewController{

override func viewDidLoad() {

super.viewDidLoad()


self.view.addSubview(btnFR)

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .width , relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 0.9, constant: 0.0))

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 0.7, constant: 0.0))


self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0.0))

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0.0))

}


var btnFR : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.backgroundColor = UIColor.red
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(ViewController.pressBtnFR), for: UIControlEvents.touchUpInside)
return btn
}()

func pressBtnFR() {
print("Test")
}
}


Add the button to the Self view. It is Missing






share|improve this answer














class ViewController:UIViewController{

override func viewDidLoad() {

super.viewDidLoad()


self.view.addSubview(btnFR)

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .width , relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 0.9, constant: 0.0))

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 0.7, constant: 0.0))


self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0.0))

self.view.addConstraint(NSLayoutConstraint(item: btnFR, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0.0))

}


var btnFR : UIButton = {
let btn = UIButton()
btn.setTitle("", for: UIControlState.normal)
btn.backgroundColor = UIColor.red
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(ViewController.pressBtnFR), for: UIControlEvents.touchUpInside)
return btn
}()

func pressBtnFR() {
print("Test")
}
}


Add the button to the Self view. It is Missing







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 3:29

























answered Nov 21 at 3:22









Ruban4Axis

17313




17313












  • I added it, I forgot to paste it on my topic
    – Jodonné Yazigi
    Nov 21 at 14:13


















  • I added it, I forgot to paste it on my topic
    – Jodonné Yazigi
    Nov 21 at 14:13
















I added it, I forgot to paste it on my topic
– Jodonné Yazigi
Nov 21 at 14:13




I added it, I forgot to paste it on my topic
– Jodonné Yazigi
Nov 21 at 14:13


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404686%2faction-uibutton-programmatically-swift-3%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen