触摸一次后隐藏和取消隐藏按钮,如何使UIButton无法点击? - 迅捷

时间:2017-11-02 23:46:56

标签: ios swift uibutton

我做了一个测验应用。 1)如果每个问题被触摸一次,我希望带有答案的按钮是不可点击的.2)每当用户移动到下一个答案时,我也希望隐藏和取消隐藏“下一个问题”按钮。例如问题1.隐藏下一个问题按钮 - >用户触摸答案 - >所有答案按钮都变得不可点击( 1)我想这样做) - >下一个问题按钮未隐藏( 2)我想这样做)。

import UIKit

struct Question {
    var Question: String!
    var Answers: [String]!
    var Answer: Int!

    init(item: [String: Any])
    {
        self.Question = item["Question"] as? String
        self.Answers = item["Answers"] as? [String]
        self.Answer = item["Answer"] as? Int
    }
}

class LittleTestViewController: UIViewController {

    //MARK: Properties
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet var buttons: [UIButton]!
    @IBOutlet weak var Next: UIButton!

    var Questions = [Question]()
    var QNumber = Int()
    var answerNumber = Int()
    var score = Int()

    override func viewDidLoad() {
        super.viewDidLoad()
        Hide()
        jsonParsingQuestionsFile()
        pickQuestion()
    }

    func jsonParsingQuestionsFile ()
    {
        if let path = Bundle.main.path(forResource: "data", ofType: "json")
        {
            if let array = (try? JSONSerialization.jsonObject(with: Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe), options: JSONSerialization.ReadingOptions.allowFragments)) as? [[String : Any]]
            {
                for item in array
                {   
                    self.Questions.append(Question(item: item))
                }
            }
        }
    }

    func pickQuestion ()
    {

        if Questions.count > 0 {
            QNumber = 0

            questionLabel.text = Questions[QNumber].Question

            answerNumber = Questions[QNumber].Answer

            for i in 0..<buttons.count{
                buttons[i].setTitle(Questions[QNumber].Answers[i], for: UIControlState.normal)
            }
            Questions.remove(at: QNumber)
        }
        else
        {
           let alert = UIAlertController(title: "Σκόρ", message: "Απάντησες σωστάς τις \(score) ερωτήσεις από τις 3", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Μενού", style: UIAlertActionStyle.default, handler: { action in
                self.navigationController?.popToRootViewController(animated: true)
            }))
            self.present(alert, animated: true, completion: nil)
        }
    }

    func Hide()
    {
        Next.isHidden = true
    }

    func Unhide()
    {
        Next.isHidden = false
    }

    @IBAction func bt1(_ sender: UIButton) {
        Unhide()
        if answerNumber == 0 {
            sender.backgroundColor = UIColor.green
            score+=1
        }
        else
        {
            sender.backgroundColor = UIColor.red
        }
    }
    @IBAction func btn2(_ sender: UIButton) {
        Unhide()
        if answerNumber == 1 {
            sender.backgroundColor = UIColor.green
            score+=1
        }
        else
        {
            sender.backgroundColor = UIColor.red
        }
    }
    @IBAction func btn3(_ sender: UIButton) {
        Unhide()
        if answerNumber == 2 {
            sender.backgroundColor = UIColor.green
            score+=1
        }
        else
        {
            sender.backgroundColor = UIColor.red
        }
    }
    @IBAction func btn4(_ sender: UIButton) {
        Unhide()
        if answerNumber == 3 {
            sender.backgroundColor = UIColor.green
            score+=1
        }
        else
        {
            sender.backgroundColor = UIColor.red
        }
    }

    @IBAction func Next(_ sender: Any)
    {
        pickQuestion()
    }

}

2 个答案:

答案 0 :(得分:2)

.isHidden:会将您的按钮设置为可见或不可见(不会处理事件或其中的任何内容,而不是那里)

.isEnable:将按钮设置为正常状态或正常状态但有点透明(alpha 0.7 +/-)并且不会被点击(不会将触摸传递给超级视图)

PD:建议您使用“guard”而不是嵌套if

func jsonParsingQuestionsFile ()
    {
        if let path = Bundle.main.path(forResource: "data", ofType: "json")
        {
            if let array = (try? JSONSerialization.jsonObject(with: Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe), options: JSONSerialization.ReadingOptions.allowFragments)) as? [[String : Any]]
            {
                for item in array
                {   
                    self.Questions.append(Question(item: item))
                }
            }
        }
    }

到此:

func jsonParsingQuestionsFile ()
{
    guard let path = Bundle.main.path(forResource: "data", ofType: "json"),
          let array = (try? JSONSerialization.jsonObject(with: Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe), options: JSONSerialization.ReadingOptions.allowFragments)) as? [[String : Any]] else{
        return
    }

    for item in array
    {   
       self.Questions.append(Question(item: item))
    }
}

同时

您的按钮的IBActions是相同的,您应该对每个按钮操作使用相同的操作,只需将所有按钮操作链接到您的控制器中的相同IBAction代码。如果需要(不是你的情况)使用按钮标签按下不同的女巫按钮。

答案 1 :(得分:1)

要使按钮不可点击:

myAnswerButton.isEnabled = false

隐藏下一个问题按钮:

myNextQuestionButton.isHidden = true

取消隐藏下一个问题按钮:

myNextQuestionButton.isHidden = false

定义按钮,如:

@IBOutlet weak var btn1: UIButton!
@IBOutlet weak var btn2: UIButton!
@IBOutlet weak var btn3: UIButton!
@IBOutlet weak var btn4: UIButton!

创建一个禁用所有按钮的功能:

private func disableAllAnswerButtons() {
    btn1.isUserInteractionEnabled = false
    btn2.isUserInteractionEnabled = false
    btn3.isUserInteractionEnabled = false
    btn4.isUserInteractionEnabled = false
}

您需要使用其他功能再次启用:

private func enableAllAnswerButtons() {
    btn1.isUserInteractionEnabled = true
    btn2.isUserInteractionEnabled = true
    btn3.isUserInteractionEnabled = true
    btn4.isUserInteractionEnabled = true
}

在取消隐藏功能中禁用它们:

func Unhide()
    {
        Next.isHidden = false
        disableAllAnswerButtons()
    }

按下下一个问题按钮时再次启用它们:

 @IBAction func Next(_ sender: Any)
    {
        pickQuestion()
        enableAllAnswerButtons()
    }