[英]Can't add a textfield in AlertController
var textFD = UITextField.init()
var tempStr : String!
var datax = [Tasks]()
var tempArr = [NSManagedObject]()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "Add", style: UIBarButtonItemStyle.plain, target: self, action:#selector(myTableViewController.addFunc))
print("Hello")
fetchData()
}
func addFunc(){
let alertO = UIAlertController.init(title: "Add", message: "Add what you want", preferredStyle: .alert)
let saveAct=UIAlertAction.init(title: "Save", style: .default, handler:saveHandler)
let cancelAct=UIAlertAction.init(title: "Cancel", style: .default, handler: nil)
alertO.addAction(saveAct)
alertO.addAction(cancelAct)
alertO.addTextField(configurationHandler: { (textFD) in
textFD.placeholder="Hey therer"
print("textfd text1 is \n\n\n" ,textFD.text!)
self.tempStr=textFD.text!
})
print("textfd text2 is \n\n\n",textFD.text! )
self.present(alertO, animated: true, completion: nil)
self.tableView.reloadData()
self.tempStr=textFD.text!
}
func saveHandler(alert:UIAlertAction){
print("textfd text3 is \n\n\n",textFD.text! )
print("Value of tempStr \n\n ",tempStr)
let taskX = Tasks(context: context)
taskX.mydata = tempStr
appDelegate.saveContext()
self.tableView.reloadData()
print("Value of tempStr \n\n ",tempStr)
}
When i click on button addFunc will call for alert and from an textfield over alert controller , later the text of textfield is stored in a string . 当我单击按钮时,addFunc将调用警报,并通过警报控制器从文本字段中进行调用,此后,文本字段的文本存储在字符串中。
Alertcontroller, textfield over alert and placeholder of textfield is appear but when i trying to store textfield text into a string ( tempStr ) it can't stored ! 出现Alertcontroller,警报上方的文本字段和文本字段的占位符,但是当我尝试将文本字段文本存储到字符串(tempStr)中时,无法存储!
try this 尝试这个
EDIT 编辑
func addFunc(){
let alert = UIAlertController(title: "MESSAGE".localized, message: "ENTER_YOUR_MESSAGE".localized, preferredStyle: .alert)
alert.addTextField { (textField) in
// textField.placeholder = "Enter your message".localized
}
alert.addAction(UIAlertAction(title: "TITLE".localized, style: .default, handler: { (action:UIAlertAction!) in
let textField = alert.textFields![0] // Force unwrapping because we know it exists.
//print("Text field: \(String(describing: textField?.text))")
if (textField.text?.characters.count)! > 0
{
self.tempStr = textField.text
}
else
{
print("empty text")
}
}))
self.present(alert, animated: true, completion: nil)
}
Property textFD
of this class is not the one that shows on alertO
, The configurationHandler
of textField just provide a closure to help user customize textField on UIAlertController. textFD
的属性textFD
不是在alertO
上显示的alertO
,textField的configurationHandler
只是提供了一个闭包来帮助用户在UIAlertController上自定义textField。
To get text of textField on UIAlertController, first you need use property textFields
of UIAlertController instance to get which textFiled you want, then use .text
to get text of this textFiled. 要在UIAlertController上获取textField的文本,首先需要使用UIAlertController实例的属性textFields
来获取所需的textFiled,然后使用.text
来获取此textFiled的文本。
alertO.addTextField(configurationHandler: { (textFD) in
textFD.placeholder="Hey therer"
print("textfd text1 is \n\n\n" ,textFD.text!)
self.tempStr=textFD.text!
})
textfield is initializing with no text (blank default), tempStr is storing a blank value. textfield初始化时没有文本(默认为空白),tempStr存储空白值。 you will have to save string on save action. 您必须在保存操作中保存字符串。
let saveAct = UIAlertAction.init(title: "Save", style: .default, handler:{
alertAction in
let textField = alertO.textFields![0] as UITextField
self.tempStr = textField.text
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.