Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIButton未在swift中调用动作函数_Ios_Swift_Uibutton - Fatal编程技术网

Ios UIButton未在swift中调用动作函数

Ios UIButton未在swift中调用动作函数,ios,swift,uibutton,Ios,Swift,Uibutton,我有一个以编程方式创建的按钮,但由于某些奇怪的原因,当按下它时,它不会激活该操作。有几个按钮,我已经从代码中删除了其他按钮,下面是不起作用的按钮: struct Buttons { var hintButton: UIButton! let hintBtn = UIButton(frame: CGRect(x: 10, y: 20, width: 70, height: 70)) init() {hintBtn.titleEdgeInsets = UIEdgeInsets(top: 0,

我有一个以编程方式创建的按钮,但由于某些奇怪的原因,当按下它时,它不会激活该操作。有几个按钮,我已经从代码中删除了其他按钮,下面是不起作用的按钮:

struct Buttons {

 var hintButton: UIButton!
 let hintBtn = UIButton(frame: CGRect(x: 10, y: 20, width: 70, height: 70))
 init() {hintBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    hintBtn.tag = 2
    hintBtn.setTitle("Hint", forState: .Normal)
    hintBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    hintButton = hintBtn
}
} 以下是viewController中的代码:

  override func viewDidLoad() {
        super.viewDidLoad()

        AddAllGraphics()
        ButtonActions()
       self.buttons.hintBtn.enabled = true
    }

     func AddAllGraphics() {
    self.view.addSubview(buttons.hintBtn)
}

    func ButtonActions() {
      buttons.hintBtn.addTarget(self, action: "giveHint:", forControlEvents: .TouchUpInside)
    }

     func giveHint(sender:UIButton) {
        if(sender.tag == 2){
          buttons.hintBtn.enabled = false
          print("It Works")
        }
      }

我曾尝试在“giveHint”函数中设置一个断点,但当按下按钮时,它从未被调用。我已尝试将self.buttons.btn.userInteractionEnabled=true放入viewDidLoad中,但仍然不起作用。任何帮助都将不胜感激

上面的代码对我有效,但我必须正确初始化结构:

struct Buttons {
var hintButton: UIButton!
let hintBtn = UIButton(frame: CGRect(x: 10, y: 50, width: 70, height: 70))
init() {
    hintBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    hintBtn.tag = 2
    hintBtn.setTitle("Hint", forState: .Normal)
    hintBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    hintButton = hintBtn
}
}

发现了问题。该按钮位于另一个已禁用用户交互的视图后面。由于视图清晰,我以为我按下了按钮。

是否检查buttons.hintBtn是否有效?确保它有效是什么意思?与您创建的对象相同。不是nil或其他对象。我尝试了if buttons.hintBtn==nil{print“nil”}但是我得到了一个错误UIButton不能是nilWhoops,忘了在我发布的代码中输入init。它存在于实际的代码中,但操作仍然没有被调用。