Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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
如何使用swift引用文本字段?_Swift_Xcode_Sqlite_Debugging_Field - Fatal编程技术网

如何使用swift引用文本字段?

如何使用swift引用文本字段?,swift,xcode,sqlite,debugging,field,Swift,Xcode,Sqlite,Debugging,Field,我想使用alert.textFields?.last?.text和alert.textFields?.last?.text函数引用两个以上的文本字段输入,但我不确定如何引用第一个和最后一个之间的字段 let alert = UIAlertController(title: "Insert Student", message: nil, preferredStyle: .alert) alert.addTextField{ (tf) in tf.placeholder = "Nam

我想使用
alert.textFields?.last?.text
alert.textFields?.last?.text
函数引用两个以上的文本字段输入,但我不确定如何引用第一个和最后一个之间的字段

    let alert = UIAlertController(title: "Insert Student", message: nil, preferredStyle: .alert)
    alert.addTextField{ (tf) in tf.placeholder = "Name" }
    alert.addTextField{ (tf) in tf.placeholder = "House" }
    alert.addTextField{ (tf) in tf.placeholder = "Score" }

    let action = UIAlertAction (title:"Submit", style: .default) { (_) in
        guard let name = alert.textFields?.first?.text,
        let house = alert.textFields?.last?.text,
            let score = alert.textFields?.last?.text

您是否尝试添加标签?请解释我对编码是新手@Brkr@beccialert.textFields是包含所有textFields的数组。您可以获取
let name=alert.textFields[0].text;let house=alert.textFields[1].text;let score=alert.textFields[2]。text
let alert = UIAlertController(title: "Insert Student", message: nil, preferredStyle: .alert)
    alert.addTextField{ (tf) in
        tf.placeholder = "Name"
        tf.tag = 11
    }
    alert.addTextField{ (tf) in
        tf.placeholder = "House"
        tf.tag = 12
    }
    alert.addTextField{ (tf) in
        tf.placeholder = "Score"
        tf.tag = 13
    }

    let action = UIAlertAction (title:"Submit", style: .default) { (_) in
        guard let nameTextField = alert.view.viewWithTag(11) as? UITextField,
            let houseTextField = alert.view.viewWithTag(12) as? UITextField,
            let scoreTextField = alert.view.viewWithTag(13) as? UITextField else { return }

        guard let name = nameTextField.text,
            let house = houseTextField.text,
            let score = scoreTextField.text else { return }
    }