swift ios将数据分配给多个警报文本字段

时间:2018-03-30 05:29:39

标签: ios swift alert

您好我想使用提醒文本字段添加名称,日期,详细信息的数据,这怎么可能? ,就像我想分配那些"让"到每个文本字段输入 代码

func addData(){ 
//Date Formatter 
let dateFormatter = DateFormatter() 
dateFormatter.dateFormat = "yyyy/MM/dd" 

let name = "Abhirajsinh" 

let dateAdded = Date() // In Realtime it would come from datePicker 
let strDateAdded = dateFormatter.string(from: dateAdded) 

let dateExpiration = Date() // In Realtime it would come from datePicker 
let strDateExpiration = dateFormatter.string(from: dateExpiration) 

let details = "This is demo Detail" 

saveData(name: name, dateAdded: strDateAdded, dateExpiration: strDateExpiration, details: details) // USe it like this 

} 


func saveData(name:String,dateAdded:String,dateExpiration:String,details:String){ 

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 
let groceryData = GroceryItem(context: context) // Link GroceryItem & Context 
groceryData.name = name 
groceryData.dateAdded = dateAdded 
groceryData.dateExpiration = dateExpiration 
groceryData.details = details 
(UIApplication.shared.delegate as! AppDelegate).saveContext() 

}

2 个答案:

答案 0 :(得分:1)

这样做:

    // Create alert controller
    let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)

    // add textfield at index 0
    alertController.addTextField(configurationHandler: {(_ textField: UITextField) -> Void in
        textField.placeholder = "Name"

    })

    // add textfield at index 1
    alertController.addTextField(configurationHandler: {(_ textField: UITextField) -> Void in
        textField.placeholder = "Email"

    })

    // Alert action confirm
    let confirmAction = UIAlertAction(title: "OK", style: .default, handler: {(_ action: UIAlertAction) -> Void in
        print("name: \(String(describing: alertController.textFields?[0].text))")
        print("email: \(String(describing: alertController.textFields?[1].text))")
    })
    alertController.addAction(confirmAction)

    // Alert action cancel
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {(_ action: UIAlertAction) -> Void in
        print("Canelled")
    })
    alertController.addAction(cancelAction)

    // Present alert controller
    present(alertController, animated: true, completion: nil)

<强>输出:

enter image description here

答案 1 :(得分:0)

还有一个解决方案:

  private func alertWithLogin() {
    //1. Create the alert controller.
    let alert = UIAlertController(title: "Registering", message: "You are successfully register, please sign in", preferredStyle: .alert)
    //2. Add the text field. You can configure it however you need.
    alert.addTextField { (username) in
        username.text = ""
        username.placeholder = "Login:"
    }
    alert.addTextField(configurationHandler: { (passwordField) in
        passwordField.text = ""
        passwordField.placeholder = "Password:"
        passwordField.isSecureTextEntry = true
    })
    // 3. Grab the value from the text field, and print it when the user clicks OK.
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
        let userNameField = alert?.textFields![0] // Force unwrapping because we know it exists.
        let passwordFields = alert?.textFields![1]

        let username = userNameField?.text
        let password = passwordFields?.text
        if username == "" || password == "" {
            self.alert(message: "Please enter your data to field", title: "Empty field")
        } else {
            // Defining the user object
                else {
                    //Func go to main screen
                }
            })
        }
    }))
    self.present(alert, animated: true, completion: nil)
}

Output