Swift iOS:将数据分配给多个警报文本字段

6

你好,我想使用警告文本框添加姓名、日期和详细信息等数据,这是否可能?就像我想将这些“let”分配给每个文本框输入代码一样。

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() 

}

你的问题不够清晰。 - dahiya_boy
我想要 alert.addTextField(configurationHandler: textFieldHandler) 分配给每个 let。 - user9561472
你想在警告框中添加textField吗? - dahiya_boy
yeah2x。文本字段用于添加名称、日期、详细信息等。 - user9561472
2个回答

5
更多解决方案:

还有一种解决方案:

  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


先生,我想知道如何检查我的核心数据库中的字段和表。 - user9561472
在这个例子中,您只需要更改应该被检查的变量。 - Andrew
先生,你能帮我处理一下我的应用程序功能吗? - user9561472
如果您使用Slack,我可以邀请您加入我们的频道。请将您的电子邮件发送给我,我会向您发送Slack频道的邀请链接。 - Andrew
我会使用一个,你的 Slack 邮箱是什么? - user9561472

2

按照以下步骤操作:

    // 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)

输出结果:

在此输入图片描述


先生,您能提供一个例子吗? - user9561472
我想要一个动态输入。 - user9561472
在问题中添加您的完整 VC 代码,并添加屏幕截图,说明您得到了什么以及您想要的是什么。 - dahiya_boy
我只想要一个提示框,会询问姓名、日期、添加日期、详细信息,用户将提供输入,当我点击“确定”时,它将被保存。 - user9561472
@DurnKurvirck,为了完成你在答案中提出的任务,请按照我所做的方法添加更多的文本字段,根据你的需求进行调整,就像我处理两个文本字段一样。 - dahiya_boy
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接