gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:58:02 25 4
gpt4 key购买 nike

您好,我想使用警报文本字段添加名称、日期、详细信息等数据,这怎么可能? ,就像我想将那些“让”分配给每个文本字段输入代码

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

}

最佳答案

另一种解决方案:

  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

关于ios - swift ios 将数据分配给多个警报文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49568892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com