带有下拉菜单和键盘选项的警报控制器?Swift 3,Xcode 8,iOS



我在编程方面是一个巨大的初学者。

我当前有一个警报控制器设置,因此当我单击"视图控制器"中的一个按钮时,警报控制器会弹出。我可以将文本输入文本字段(在警报控制器中),当我单击"确定"(在"警报控制器"中)时,文本将显示在标签上(在我的视图控制器中)。这是我拥有的代码:

//Text button
@IBOutlet weak var TextLabel: UILabel!
@IBAction func TextButtonTapped(_ sender: UIButton) {
print("Text Button Tapped")
openTextAlert()
}
func openTextAlert() {
//Create Alert Controller
let alert9 = UIAlertController (title: "Whatever Text Your Heart Desires:", message: nil, preferredStyle: UIAlertControllerStyle.alert)
//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.TextLabel.text = textfield?.text!
}
alert9.addAction(ok9)
//Add Text Field
alert9.addTextField { (textfield: UITextField) in
    textfield.placeholder = "Whatever text you want to enter"
}
//Present Alert Controller
self.present(alert9, animated:true, completion: nil)
}

如何在警报控制器中获得多个操作,以便它也充当带有某些单词的下拉菜单,以便我不需要总是写下关键字?我都需要键入自己的单词和短语的能力以及效率的单词和短语的预设。

请帮忙!我是一个巨大的菜鸟。谢谢 :)Swift 3,Xcode 8,ios

只需使用您的代码编写它。这是您需要的吗?

编辑:您的NewButTontapped功能仅仅是为了向您展示如何设置条件以显示简单的警报或ActionSheet。在此功能中,当您单击新按钮,然后单击TextButton时,将显示您的警报。如果再次单击新按钮,则将UserWantStoShowalert设置为False。当您再次单击textButTontapped时,将调用OpenActionsHeet。

您可以以多种方式进行此操作。

@IBOutlet weak var TextLabel: UILabel!
var userWantsToShowAlert = false
@IBAction func yourNewButtonTapped(_ sender: UIButton) {
        userWantsToShowAlert = !userWantsToShowAlert 
        print("User wants to show alert? (userWantsToShowAlert)")
//This is userWantsToShowAlert is false, it will change it to true. And if it is true, it will change it to false.
    }
@IBAction func TextButtonTapped(_ sender: UIButton) {
        print("Text Button Tapped")
        if(userWantsToShowAlert){
            openTextAlert()
        }else{
            openActionSheetAlert()
        }

    }
    func openTextAlert() {
        //Create Alert Controller
        let alert9 = UIAlertController (title: "Whatever Text Your Heart Desires:", message: nil, preferredStyle: UIAlertControllerStyle.alert)
        //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.TextLabel.text = textfield?.text!
        }
        alert9.addAction(ok9)
        //Add Text Field
        alert9.addTextField { (textfield: UITextField) in
            textfield.placeholder = "Whatever text you want to enter"
        }
        //Present Alert Controller
        self.present(alert9, animated:true, completion: nil)
    }
    func openActionSheetAlert(){
        let alert9 = UIAlertController (title: "Whatever Text Your Heart Desires:", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
        //Create Cancel Action
        let cancel9 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
        alert9.addAction(cancel9)

         let bt1 = UIAlertAction(title: "1", style: UIAlertActionStyle.default){
        (action) in self.TextLabel.text = "Word 1"}
    alert9.addAction(bt1)
    let bt2 = UIAlertAction(title: "2", style: UIAlertActionStyle.default){
        (action) in self.TextLabel.text = "Word 2"}
    alert9.addAction(bt2)

    let bt3 = UIAlertAction(title: "3", style: UIAlertActionStyle.default){
        (action) in self.TextLabel.text = "Word 3"}
    alert9.addAction(bt3)
    let bt4 = UIAlertAction(title: "4", style: UIAlertActionStyle.default){
        (action) in self.TextLabel.text = "Word 4"}
    alert9.addAction(bt4)
alert9.popoverPresentationController?.sourceView = self.view
            alert9.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.size.width / 2.0, y: self.view.bounds.size.height / 2.0, width: 1.0, height: 1.0)
            self.present(alert9, animated:true, completion: nil)
        }

最新更新