I'd like to do a label which is present only when button is touched. When function hidden() is called in MainViewController It's working well but when i'm calling it from ButtonAction class (same function) I get an classic error:
Unexpectedly found nil while unwrapping an Optional value
Here's the code:
// MainViewController.swift
import UIKit
class MainViewController: UIViewController {
@IBOutlet weak var labelToShow: UILabel!
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
labelToShow.isHidden = true
}
func hidden() {
labelToShow.isHidden = true
}
func inHidden() {
labelToShow.isHidden = false
}
}
AND :
// ButtonAction.swift
import UIKit
class ButtonAction: UIButton {
var touched:Bool = false
var mainScreen = MainViewController()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
sleep(1)
mainScreen.hidden()
touched = true
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
mainScreen.inHidden()
touched = false
}
}