I am trying to show a UIAlertController with a UITextView. When I add the line:
我试图显示一个UITextView的UIAlertController。当我添加一行:
//Add text field
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
}
I get a Runtime error:
我得到一个运行时错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
致命错误:在展开可选值时意外发现nil
let alertController: UIAlertController = UIAlertController(title: "Find image", message: "Search for image", preferredStyle: .Alert)
//cancel button
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//cancel code
}
alertController.addAction(cancelAction)
//Create an optional action
let nextAction: UIAlertAction = UIAlertAction(title: "Search", style: .Default) { action -> Void in
let text = (alertController.textFields?.first as! UITextField).text
println("You entered \(text)")
}
alertController.addAction(nextAction)
//Add text field
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
textField.textColor = UIColor.greenColor()
}
//Present the AlertController
presentViewController(alertController, animated: true, completion: nil)
This is presented inside my ViewController via an IBAction.
这是通过IBAction在我的ViewController中显示的。
I have downloaded the code from here: http://sourcefreeze.com/uialertcontroller-ios-8-using-swift/ and it works fine. I copied and pasted that method into my code and it breaks. The presence of self on the last line has no impact.
我从这里下载了代码:http://sourcefreeze.com/uialertcontroller-ios-8- use -swift/,运行良好。我把那个方法复制粘贴到我的代码中,它就坏了。自我在最后一行的存在没有影响。
107
alert.addTextField(configurationHandler: { (textField) in
textField.placeholder = "Enter First Name"
})
Use this code, I am running this code in my app successfully.
使用此代码,我在我的应用程序中成功地运行这段代码。
@IBAction func addButtonClicked(sender : AnyObject){
let alertController = UIAlertController(title: "Add New Name", message: "", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
textField.placeholder = "Enter Second Name"
}
let saveAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: { alert -> Void in
let firstTextField = alertController.textFields![0] as UITextField
let secondTextField = alertController.textFields![1] as UITextField
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: {
(action : UIAlertAction!) -> Void in })
alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
textField.placeholder = "Enter First Name"
}
alertController.addAction(saveAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
Edited: Swift 3.0 version
编辑:斯威夫特3.0版本
@IBAction func addButtonClicked(_ sender: UIButton){
let alertController = UIAlertController(title: "Add New Name", message: "", preferredStyle: .alert)
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter Second Name"
}
let saveAction = UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in
let firstTextField = alertController.textFields![0] as UITextField
let secondTextField = alertController.textFields![1] as UITextField
print("firstName \(firstTextField.text), secondName \(secondTextField.text)")
})
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: { (action : UIAlertAction!) -> Void in })
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter First Name"
}
alertController.addAction(saveAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
17
To add a text field in Swift 3.0:
在Swift 3.0中添加文本字段:
let alertController = UIAlertController(title: "Title", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in
let textField = alertController.textFields![0] as UITextField
// do something with textField
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
textField.placeholder = "Search"
})
self.present(alertController, animated: true, completion: nil)
10
So I started checking to see what could possibly have been different in my code to the working code. I noticed that my ViewController extends
所以我开始检查我的代码与工作代码有什么不同。我注意到我的ViewController扩展了
UITextFieldDelegate
Which apparently means that I need to set the delegate of any child UITextView:
这显然意味着我需要设置任何子UITextView的委托:
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
searchTextField = textField
searchTextField?.delegate = self //REQUIRED
searchTextField?.placeholder = "Enter your search terms"
}
3
How to add textField to AlertView? Let's keep it short and simple.
如何向AlertView添加textField ?让我们保持简短。
This works for Swift 3.0 and above.
这适用于Swift 3.0及以上版本。
var nameField: UITextField?
let alertController = UIAlertController(title: "Add Number", message: nil, preferredStyle: .alert)
// Add textfield to alert view
alertController.addTextField { (textField) in
nameField = textField
}
First, you instantiate an object of UIAlertController
and then you add a text field to it by accessing addTextField
member of UIAlertController
class.
首先,实例化UIAlertController的对象,然后通过访问UIAlertController类的addTextField成员将文本字段添加到它。
2
add TextField to UIAlertController and TextField text Display on UILabel in Swift
将TextField添加到UIAlertController,并在UILabel上以Swift格式显示TextField文本
let alert = UIAlertController(title: "Alert", message: "", preferredStyle: .alert)
alert.addTextField { (textField) in
textField.placeholder = "First Name"
}
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
let textField = alert?.textFields![0]
self.label.text = textField?.text }))
self.present(alert, animated: true, completion: nil)
1
Great answer a slight modification to show how the textfield can be used:
伟大的答案一个微小的修改显示如何使用textfield:
func addRow (row: Int, bodin: String, flag: Int) {
let alertController = UIAlertController(title: bodin, message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: {
alert -> Void in
_ = alertController.textFields![0] as UITextField
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
switch flag {
case 0:
textField.keyboardType = UIKeyboardType.phonePad
textField.placeholder = "Enter Number"
case 1:
textField.keyboardType = UIKeyboardType.emailAddress
textField.placeholder = "Enter Email"
default:
break
}
})
0
For Swift 4.0, You can use this sample of code succesfully tested in my project:
对于Swift 4.0,您可以在我的项目中使用这个成功测试过的代码示例:
@IBAction func withdrawTapped(_ sender: UIButton) {
let alertController = UIAlertController(title: "Token withdraw", message: "", preferredStyle: .alert)
let withdrawAction = UIAlertAction(title: "Withdraw", style: .default) { (aciton) in
let text = alertController.textFields!.first!.text!
if !text.isEmpty {
self.presentAlert(
title: "Succesful",
message: "You made request for withdraw \(textField.text) tokens")
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
}
alertController.addTextField { (textField) in
textField.placeholder = "999"
textField.keyboardType = .decimalPad
}
alertController.addAction(withdrawAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/08/10/80505a23205f0558def5fefd58cb0c05.html。