从UIPickerview获取文本并在UIAlert

时间:2017-01-16 20:25:45

标签: ios swift uitextfield

Here's an example。我有其他关于文本字段的警报和其他具有选择器视图的警报但是同时使用两者都是一种痛苦。

let gradeTypeTextField = gradeTypeTextField (Doesn't work)

var gradeTypeTextField: UITextField!

@IBAction func newGrade(_ sender: AnyObject) {
 When button is tapped, show alert.
    alertForGrade()
}

var gradeType = ["Attendance", "Assignment", "Homework Assignment", "Quiz", "Test", "Mid-Term", "Exam", "Other"]


override func viewDidLoad() {
    super.viewDidLoad()

     Hide extra cells
    tableView.tableFooterView = UIView()

}


func alertForGrade() {

     Create the alert controller.
 alert = UIAlertController(title: "New Grade", message: nil, preferredStyle: .alert);


    Add the text field. You can configure it however you need.
    alert?.addTextField(configurationHandler: { (gradeTypeTextField) -> Void in

        gradeTypeTextField.placeholder = "Type of Grade."

        let pickerView = UIPickerView()
        pickerView.delegate = self
        pickerView.dataSource = self
        pickerView.backgroundColor = UIColor.white
        pickerView.showsSelectionIndicator = true
        gradeTypeTextField.inputView = pickerView
        self.view.endEditing(true)

    })

    alert?.addTextField(configurationHandler: { (assignmentTextField) -> Void in
        assignmentTextField.placeholder = "Name of the assignment."

    })

    alert?.addTextField(configurationHandler: { (gradeTextField) -> Void in gradeTextField.placeholder = "Grade for the assignment."

        Grab the value from the text field, and print it when the user clicks OK.
        let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
            let assignmenttextField = (self.alert?.textFields![0])! as UITextField

            let gradeTextField = (self.alert?.textFields![1])! as UITextField
            let assignment = assignmenttextField.text!

            let gradeString = gradeTextField.text!

            let grade = Int(gradeString)

            self.dictionaryForGradebook.updateValue(grade!, forKey: assignment)

            print(self.dictionaryForGradebook)

            self.tableView.reloadData()
        })


        self.alert?.addAction(ok)
        self.alert?.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in


        }))

        // 4. Present the alert.
        self.present(self.alert!, animated: true, completion: nil)


    })


}


func numberOfComponents(in pickerView: UIPickerView) -> Int {

    return 1
}

 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    return gradeType.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    return gradeType[row]
}


func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

This prints but if I try " yearTextField.text = years[row]" it crashes.
           print("Testinggggg") 

    self.view.endEditing(true)


}

isuue在哪里?

1 个答案:

答案 0 :(得分:0)

当您在UITextField中选择行时尝试向其添加文本时,您需要能够引用创建的UIPicker。你似乎忘了放置这个链接:

alert?.addTextField(configurationHandler: { (gradeTypeTextField) -> Void in

    // Append this: 
    self.gradeTypeTextField = gradeTypeTextField

    gradeTypeTextField.placeholder = "Type of Grade."

    let pickerView = UIPickerView()
    pickerView.delegate = self
    pickerView.dataSource = self
    pickerView.backgroundColor = UIColor.white
    pickerView.showsSelectionIndicator = true
    gradeTypeTextField.inputView = pickerView
    self.view.endEditing(true)

})

然而,这将创建一个保留周期,因此建议通过将[weak self]添加到闭包的开头来创建无主/弱自我链接。