ios - Swift 多项选择输入答案

标签 ios swift

我想知道是否有人可以帮我回答以下问题。此代码的情况一将询问名称,您可以单击四个选项之一来选择正确的答案。我想知道如何才能输入问题的答案,例如:

What is my name?

My name is "Type answer here"

"Cesar"
"Karlos"
"William"
"Chiqui"

这是我现有的代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var QuestionLabel: UILabel!
    @IBOutlet weak var Button1: UIButton!
    @IBOutlet weak var Button2: UIButton!
    @IBOutlet weak var Button3: UIButton!
    @IBOutlet weak var Button4: UIButton!
    @IBOutlet weak var Next: UIButton!
    @IBOutlet weak var LabelEnd: UILabel!
    
    var CorrectAnswer = String()
    var randomQuestionArray: [Int] = [1, 2, 3, 4]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
       
        
        RamdomQuestions()
            }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func RamdomQuestions () {
        let randomIndex = Int(arc4random_uniform(UInt32(randomQuestionArray.count)))
        
        switch (randomQuestionArray[randomIndex]) {
            
        case 1:
            QuestionLabel.text = "What is my name? "
            Button1.setTitle ("Cesar", forState: UIControlState.Normal)
            Button2.setTitle ("Karlos", forState: UIControlState.Normal)
            Button3.setTitle ("William", forState: UIControlState.Normal)
            Button4.setTitle ("Chiqui", forState: UIControlState.Normal)
            CorrectAnswer = "2"
            
            break
        case 2:
            QuestionLabel.text = "What is my last name? "
            Button1.setTitle ("Perez", forState: UIControlState.Normal)
            Button2.setTitle ("Carvajal", forState: UIControlState.Normal)
            Button3.setTitle ("Garcia", forState: UIControlState.Normal)
            Button4.setTitle ("Sanchez", forState: UIControlState.Normal)
            CorrectAnswer = "1"
            break
        case 3:
            QuestionLabel.text = "What is my favorite dish? "
            Button1.setTitle ("Pasta", forState: UIControlState.Normal)
            Button2.setTitle ("Fish", forState: UIControlState.Normal)
            Button3.setTitle ("Vegetables", forState: UIControlState.Normal)
            Button4.setTitle ("McMc", forState: UIControlState.Normal)
            CorrectAnswer = "1"

            break
        case 4:
            QuestionLabel.text = "What is my favorite color"
            Button1.setTitle ("Red", forState: UIControlState.Normal)
            Button2.setTitle ("Blue", forState: UIControlState.Normal)
            Button3.setTitle ("Orange", forState: UIControlState.Normal)
            Button4.setTitle ("Black", forState: UIControlState.Normal)
            CorrectAnswer = "4"

            
            break
    
        default:
            break
        }
       randomQuestionArray.removeAtIndex(randomIndex)
    
    }
    func Hide (){
        LabelEnd.hidden = true
        Next.hidden = true
    }
    func UnHide () {
        LabelEnd.hidden = false
        Next.hidden = false
    }
    
    @IBAction func Button1Action(sender: AnyObject) {
        UnHide()
        if (CorrectAnswer == "1") {
            LabelEnd.text = "Correcto"
        }
        else{
        LabelEnd.text = "Falso"
        
            }
    }
     func Button2Action(sender: AnyObject) {
        UnHide()
        if (CorrectAnswer == "2") {
            LabelEnd.text = "Correcto"

    }
        else{
            LabelEnd.text = "Falso"

        }
    }
    
    func Button3Action(sender: AnyObject) {
        UnHide()
        if (CorrectAnswer == "3") {
            LabelEnd.text = "Correcto"

        }
        else{
           LabelEnd.text = "Falso"
 
    }
    }

     func Button4Action(sender: AnyObject) {
        UnHide()
            if (CorrectAnswer == "4") {
            LabelEnd.text = "Correcto"
            
        }
    
        else{
            LabelEnd.text = "Falso"
    }
    }
    @IBAction func Next(sender: AnyObject) {
        RamdomQuestions()
    }
}

最佳答案

您可以使用 UITextField 输入问题的答案。但是,这不是一个好方法,因为您必须处理所有大写/小写字母场景。 例如-用户可以通过以下方式输入名称:

"Cesar", "cesar", "CESAR"

为了避免所有这些处理,最好使用 UIButton。

关于ios - Swift 多项选择输入答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37825648/

相关文章:

ios - SVProgressHUD 在显示 UITableView 之前不显示

ios - CollectionView 滚动时更改单元格背景图像

ios - 自定义 uitableviewcell 突出显示颜色

ios - 在 TodayExtension 中使用 MagicalRecord (CoreData)

ios - 是否可以将分段控件标题设置为左对齐?

ios - 从 Swift 函数中的异步调用返回数据

ios - 条件绑定(bind)的初始化程序必须具有 Optional 类型,而不是 'NSManagedObjectContext

ios - 从按钮执行 segue 时应用程序卡住

ios外部显示以编程方式

ios - 生成导致丢帧的新节点 [SpriteKit]