Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.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 uicontrol状态的图像。未显示选定的_Ios_Swift_Selected_Uicontrolstate - Fatal编程技术网

Ios uicontrol状态的图像。未显示选定的

Ios uicontrol状态的图像。未显示选定的,ios,swift,selected,uicontrolstate,Ios,Swift,Selected,Uicontrolstate,标题说明了一切。这是我的密码: func createCheckBoxButton(xPos: CGFloat, yPos: CGFloat, tag: Int) -> UIButton { var checkBox = UIButton(frame: CGRect(x: xPos, y: yPos, width: checkBoxSize, height: checkBoxSize)) checkBox.setBackgroundImage(UIImage(named:

标题说明了一切。这是我的密码:

func createCheckBoxButton(xPos: CGFloat, yPos: CGFloat, tag: Int) -> UIButton {
    var checkBox = UIButton(frame: CGRect(x: xPos, y: yPos, width: checkBoxSize, height: checkBoxSize))
    checkBox.setBackgroundImage(UIImage(named: "checkbox_inactive"), forState: UIControlState.Normal)
    checkBox.setBackgroundImage(UIImage(named: "checkbox_pressed"), forState: UIControlState.Highlighted)
    checkBox.setBackgroundImage(UIImage(named: "checkbox_active"), forState: UIControlState.Selected)
    checkBox.tag = tag
    checkBox.contentMode = .ScaleAspectFit
    checkBox.addTarget(self, action: "processButton:", forControlEvents: UIControlEvents.TouchUpInside)
    return checkBox
}
当按下我的按钮时,有一个被调用的函数:

func processButton(sender: UIButton) {
    if (answerViewArray[sender.tag].backgroundColor == UIColor.whiteColor()) {
        answerViewArray[sender.tag].backgroundColor = myColor.pinky()
    } else {
        answerViewArray[sender.tag].backgroundColor = UIColor.whiteColor()
    }
    let tag = answerButtonsArray[sender.tag]
    answer.buttonPressed(tag)
}
当我启动应用程序时,会出现
复选框\u inactive
图像。当我按下并保持按下时,会出现
复选框\u pressed
图像。但当我松开鼠标时,会再次出现
复选框\u inactive
,而不是
复选框\u active

我还尝试了使用
UIImageView
,这实际上是对我来说最好的解决方案。我将复选框设置为
UIImageView
,并在常规视图的顶部放置一个不可见的视图,以便我可以在任何地方单击。但是当我按下我的不可见视图时,
UIImageView
就消失了

代码如下:

func createCheckBoxButton(xPos: CGFloat, yPos: CGFloat) -> UIImageView {
    var checkBox = UIImageView(frame: CGRect(x: xPos, y: yPos, width: checkBoxSize, height: checkBoxSize))
    checkBox.image = UIImage(named: "checkbox_inactive")
    checkBox.contentMode = .ScaleAspectFit
    return checkBox
}
以下是名为:

func processButton(sender: UIButton) {
    if (answerViewArray[sender.tag].backgroundColor == UIColor.whiteColor()) {
        answerViewArray[sender.tag].backgroundColor = myColor.pinky()
        checkBoxArray[sender.tag].image = UIImage(named: "checkbox-active")
    } else {
        answerViewArray[sender.tag].backgroundColor = UIColor.whiteColor()
        checkBoxArray[sender.tag].image = UIImage(named: "checkbox-inactive")
    }

    let tag = answerButtonsArray[sender.tag]

    answer.buttonPressed(tag)
}

若要在释放按钮时激活“显示”复选框,应为按下的按钮设置“选中=真”

因此,您的函数应该如下所示:

func processButton(sender: UIButton) {
  // If button not selected
  if(sender.selected==false){
    sender.selected = true;
  }
  else{ // If button already selected
    sender.selected = false;
  }

  // Do your other stuff
}

请原谅我的无知,我对斯威夫特很陌生:D@Magohamoth对于您的第二个问题,我不能肯定地说什么,因为您的代码并没有完全满足调试的所有要求,但若您不想创建类似于复选框的视图,那个么UIButton是最好的方法。