ios - 带有下拉菜单和键盘选项的警报 Controller ? swift 3、Xcode 8、IOS

标签 ios drop-down-menu swift3 uialertcontroller

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

我目前设置了一个警报 Controller ,因此当我单击 View Controller 中的按钮时 an alert controller pops up .我可以在文本字段中输入文本(在警报 Controller 中),当我单击“确定”(在警报 Controller 中)时,文本将显示在标签上(在我的 View Controller 中)。这是我所拥有的代码:

//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)
}

如何在我的警报 Controller 中执行多个操作,以便它也充当 a dropdown menu with certain words so that I don't need to always write out key words?我既需要能够输入自己的单词和短语,也需要预设单词和短语以提高效率。

求助!我是个大菜鸟。谢谢 :) Swift 3、Xcode 8、IOS

最佳答案

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

编辑:yourNewButtonTapped 函数只是向您展示如何设置条件来显示简单警报或操作表。在此功能中,当您单击新按钮,然后单击 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)
        }

关于ios - 带有下拉菜单和键盘选项的警报 Controller ? swift 3、Xcode 8、IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42369519/

相关文章:

html - CSS 下拉菜单(阻止父元素在子元素处于事件状态时处于事件状态)

javascript - 两个下拉过滤器。第一个确定第二个中的信息

swift - 使用 Firebase 异步编写代码

swift - Swift 中的隐式类型转换

ios - 将 UIButton 移动到键盘上方

ios - 如何在 XCode 6 中并排水平居中放置 2 个标签?

iphone - 如何使用大气着色器为iOS制作逼真的3D地球

ios - 捏和旋转手势结束后如何将图像设置为原始状态?

Javascript检测下拉列表何时关闭

swift - 通过拖放重新排序 NSCollectionView 中的项目,swift 3+