基本上,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它將該文本插入視圖控制器中的標籤。我擴展了這種能力,以便我可以有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.我在想,如果你還可以編輯我選擇的關鍵詞?每次我嘗試添加文本到標籤中的任何文本時,我都必須從空白文本框開始。標籤 - > Alert Controller Textfield?我可以在警報控制器的文本字段中編輯標籤中的預先存在的文本嗎? Swift 3,Xcode 8,IOS
具體來說,無論我的標籤中是否有文本,我可以在文本框中準備好文本以進行編輯,下次我拉起警報控制器?
這是我到目前爲止,第二圖像:
//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)
}
請幫幫忙!我對Xcode和編程一般都很陌生,所以在我看來,這是一個很大的白癡。
謝謝:)
沒有違法替換佔位符,但如果你在你的代碼仔細看它的死簡單地實現這一點。只需將'textfield.text'屬性設置爲所需值而不是設置其佔位符。就那麼簡單。 – Losiowaty
我試過「textfield.text = self.UselessLabel.text」,並且工作,但只做了一半的工作。它允許我編輯標籤中的現有單詞,但是如何將預設關鍵字添加到標籤中的任何內容?當我點擊一個預設關鍵字時,問題就出現了,它只會在標籤中顯示該關鍵字,並刪除之前的任何內容。 – DanielG
啊,這實際上是有點棘手:)你需要修改你的動作處理程序,以便他們追加文本,而不是設置預定義的常量。如果你谷歌「斯威夫特追加標籤」你一定會找到有用的;) – Losiowaty