Home > Software design >  My buttons aren’t working and I don’t know why [duplicate]
My buttons aren’t working and I don’t know why [duplicate]

Time:09-24

I am trying to make a truth or dare game as my first project, I got no errors but the buttons just don't work I ran the code step by step and when I press the buttons they don't work truthsEng and DaresEng are the truths and dares they are in the share source folder.

import PlaygroundSupport
import UIKit
let view = UIView()
//adding the baground image
let Game = UIImageView(frame: UIScreen.main.bounds)
Game.image = UIImage(named:"Photo.png")
Game.contentMode = .scaleAspectFill
view.insertSubview(Game, at:0)
let lbl = UILabel(frame: CGRect(x:100 , y:100, width: 300, height: 500))
lbl.text = "test"
lbl.textColor = #colorLiteral(red: 0.6313725709915161, green: 0.6470588445663452, blue: 0.9058823585510254, alpha: 1.0)
Game.addSubview(lbl)
let truthButton = UIButton(frame: CGRect(x:100, y: 100, width: 100, height: 69))
truthButton.layer.cornerRadius = 10
truthButton.backgroundColor = #colorLiteral(red: 0.6325997710227966, green: 0.6475543975830078, blue: 0.905613362789154, alpha: 1.0)
truthButton.setTitle("truth", for: UIControl.State.normal)
Game.addSubview(truthButton)
let dareButton = UIButton(frame: CGRect(x:300, y: 400, width: 100, height: 69))
dareButton.layer.cornerRadius = 10
dareButton.backgroundColor = #colorLiteral(red: 0.6352941393852234, green: 0.6470588445663452, blue: 0.886274516582489, alpha: 1.0)
dareButton.setTitle("dare", for: UIControl.State.normal)
Game.addSubview(dareButton)

class Responder: NSObject{
    @objc func PrintTruthAndChangeBaground() {
        lbl.text = truthsEng.randomElement()
        
    }
    @objc func PrintDareAndChangeBaground() {
        lbl.text = daresEng.randomElement()
        
    }
    
}
let responser = Responder()
truthButton.addTarget(responser, action: #selector(Responder.PrintTruthAndChangeBaground), for:.touchUpInside)
dareButton.addTarget(responser, action: #selector(Responder.PrintDareAndChangeBaground), for: .touchUpInside)

PlaygroundPage.current.liveView = Game

CodePudding user response:

UIKit UIImageView isUserInteractionEnabled

A Boolean value that determines whether user events are ignored and removed from the event queue.

Declaration var isUserInteractionEnabled: Bool { get set }

Discussion This property is inherited from the UIView parent class. This class changes the default value of this property to false.

You need set this property to true.

  • Related