1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
import UIKit
class ViewController: UIViewController {
var nom: String = "Jean"
let buttonClick: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.layer.borderWidth = 2
button.layer.borderColor = UIColor(red: 0/255, green: 174/255, blue: 239/255, alpha: 1).cgColor
button.setTitle("test", for: .normal)
button.setTitleColor(UIColor(red: 0/255, green: 174/255, blue: 239/255, alpha: 1), for: .normal)
button.addTarget(self, action: #selector (resultat(_:)), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.view.addSubview(buttonClick)
redessine(UIScreen.main.bounds.size)
}
//MARK: - Affichage
func redessine(_ size: CGSize) {
let views = ["buttonClick": buttonClick]
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(250)-[buttonClick]-(>=20)-|", options: [], metrics: nil, views: views))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(200)-[buttonClick]-(200)-|", options: [], metrics: nil, views: views))
}
//MARK: - Fonctions
@objc func resultat(_ sender: UIButton) {
if nom == "Jean" {
let deuxiemeVue = DeuxiemeViewController()
self.present(deuxiemeVue, animated:true, completion:nil)
}else{
let alertController = UIAlertController(title: "Attention", message: "Ceci est une alerte", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)
present(alertController, animated: true, completion: nil)
}
}
} |
Partager