如何单击uialertcontroller操作以将文本输入/编辑到uialertcontroller文本字段中? Swift 3,Xcode 8,IOS

时间:2017-02-28 07:30:34

标签: ios swift3 uitextfield uialertcontroller

基本上,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.我想知道你是否也可以编辑我选择的那些关键词?因此,我可以将其发送到我的文本区域,编辑它,然后使用确定按钮将其发送到我的标签,而不是单击我的预设单词并将其发送到我的标签,而不是单击它?

//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和一般的编程都很陌生,所以当谈到这一点时,我是一个很大的白痴。

谢谢:)

0 个答案:

没有答案