[英]Label -> Alert Controller Textfield? Can I edit pre-existing text from a label, in a textfield in an alert controller? Swift 3, Xcode 8, IOS
Basically, i have an alert controller set up so that when i click a button in the view controller, an alert controller pops up and i can type words into a textfield, click an "OK" button and it inserts that text into a label in the view controller. 基本上, 我有一个警报控制器设置,以便当我单击视图控制器中的按钮时,会弹出一个警报控制器,我可以在文本字段中键入单词,单击“确定”按钮 ,然后将该文本插入到标签中视图控制器。 Ive extended this ability so that i can have preset keywords (like "Good," "Likely" and "Almost") that i can select from in the alert controller to speed up the typing process, as these words are typed a lot in my app.
我已经扩展了这个能力,以便我可以预设关键字(如“好”,“可能”和“几乎”),我可以在警报控制器中选择,以加快打字过程,因为这些单词在我的输入很多应用程序。 I was wondering if you could also edit those key words that I've selected?
我想知道你是否也可以编辑我选择的那些关键词? Every time i try to add text to whatever text is in the label i have to start from a blank textfield.
每当我尝试将文本添加到标签中的任何文本时,我必须从空白文本字段开始。
Specifically, whatever text that is already in my label, can i have that text ready in my textfield to edit, next time i pull up the alert controller? 具体来说,无论我的标签中是否有任何文字,我可以在文本字段中准备好该文本进行编辑,下次我拉出警报控制器吗?
This is what i have so far, for the second image: 这是我到目前为止的第二张图片:
//Editable Text Box with Preset Keywords
@IBOutlet weak var UselessLabel: UILabel!
@IBAction func UselessTapped(_ sender: UIButton) {
print("Useless Button Tapped")
openUselessAlert()
}
func openUselessAlert() {
//Create Alert Controller
let alert9 = UIAlertController (title: "Uselss:", message: nil, preferredStyle: UIAlertControllerStyle.alert)
//preset keyword as button in alert controller
let bt1 = UIAlertAction(title: "Good", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Good"}
alert9.addAction(bt1)
//preset keyword as button in alert controller
let bt2 = UIAlertAction(title: "Likely", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Likely"}
alert9.addAction(bt2)
//preset keyword as button in alert controller
let bt3 = UIAlertAction(title: "Almost", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Almost"}
alert9.addAction(bt3)
//Create Cancel Action
let cancel9 = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.cancel, handler: nil)
alert9.addAction(cancel9)
//Create OK Action
let ok9 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK")
let textfield = alert9.textFields?[0]
print(textfield?.text!)
self.UselessLabel.text = textfield?.text!
}
alert9.addAction(ok9)
//Add Text Field
alert9.addTextField { (textfield: UITextField) in
textfield.placeholder = "Useless"
}
//Present Alert Controller
self.present(alert9, animated:true, completion: nil)
}
Please help! 请帮忙! Im really new to Xcode and programming in general, so I'm a huge idiot when it comes to this.
我对Xcode和一般的编程都很陌生,所以当谈到这一点时,我是一个很大的白痴。
Thanks :) 谢谢 :)
Just replace the placeholder with text 只需用文本替换占位符即可
//Editable Text Box with Preset Keywords
@IBOutlet weak var UselessLabel: UILabel!
@IBAction func UselessTapped(_ sender: UIButton) {
print("Useless Button Tapped")
openUselessAlert()
}
func openUselessAlert() {
//Create Alert Controller
let alert9 = UIAlertController (title: "Uselss:", message: nil, preferredStyle: UIAlertControllerStyle.alert)
//preset keyword as button in alert controller
let bt1 = UIAlertAction(title: "Good", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Good"}
alert9.addAction(bt1)
//preset keyword as button in alert controller
let bt2 = UIAlertAction(title: "Likely", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Likely"}
alert9.addAction(bt2)
//preset keyword as button in alert controller
let bt3 = UIAlertAction(title: "Almost", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Almost"}
alert9.addAction(bt3)
//Create Cancel Action
let cancel9 = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.cancel, handler: nil)
alert9.addAction(cancel9)
//Create OK Action
let ok9 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK")
let textfield = alert9.textFields?[0]
print(textfield?.text!)
self.UselessLabel.text = textfield?.text!
}
alert9.addAction(ok9)
//Add Text Field
alert9.addTextField { (textfield: UITextField) in
textfield.text = "Useless"
}
//Present Alert Controller
self.present(alert9, animated:true, completion: nil)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.