简体   繁体   English

标签 - >警报控制器文本字段? 我可以在警报控制器的文本字段中编辑标签中的预先存在的文本吗? Swift 3,Xcode 8,IOS

[英]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.

相关问题 是否可以从当前警报控制器(或操作表中的文本字段)内部调用警报控制器? Xcode 8,Swift 3,IOS - Is there a way to call an alert controller from inside a current alert controller (or a textfield in an action sheet)? Xcode 8, Swift 3, IOS 如何从警报的文本字段中获取文本并将其设置为单元格的文本标签? - How can I get the text from the alert's textfield and make it the cell's text label? 隐藏警报控制器之前检查文本字段中的值-iOS Swift - Checking the value in textfield before alert controller hides - iOS swift 如何在警报控制器的文本字段中设置数字键盘[swift] - How to have a numberpad in a textfield of an alert controller [swift] 快速将文本字段传递到第二个视图控制器中的标签 - Pass textfield to a label in a second view controller in swift 带SpriteKit的iOS如何编辑预先存在的标签? - IOS w/ SpriteKit How to edit a pre-existing label? 使用Swift将输入从TextField打印到Xcode中的Label - Printing input from TextField to a Label in Xcode with Swift ios Swift 2:扩展名-带有文本字段的警报 - ios Swift 2: extension - Alert with textfield 当警报 controller 被解除时,如何让 label 文本恢复为原始成本 - How can I have the label text revert to original cost when alert controller is dismissed 从 Swift 中的 iOS 警报中的 TextField 获取输入值 - Get input value from TextField in iOS alert in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM