Swift2 等待用户完成对Swift中弹出对话框的响应

Swift2 等待用户完成对Swift中弹出对话框的响应,swift2,Swift2,我希望用户在弹出对话框中键入文本,但我希望程序等待用户在弹出对话框中完成写入文本使用UIAlertController: 斯威夫特4 这听起来像是在显示控制器而不是在用户选择警报操作时进行完成处理 您将需要尝试以下内容: import UIKit class MyViewController: UIViewController { var someStringVariable:String = "" func presentAnAlert() { le

我希望用户在弹出对话框中键入文本,但我希望程序等待用户在弹出对话框中完成写入文本

使用UIAlertController:

斯威夫特4 这听起来像是在显示控制器而不是在用户选择警报操作时进行完成处理

您将需要尝试以下内容:

import UIKit

class MyViewController: UIViewController
{
    var someStringVariable:String = ""

    func presentAnAlert()
    {
        let alert = UIAlertController(
                title: "Title",
                message: "Message",
                preferredStyle: .actionSheet //choose which style you prefer
        )

        alert.addTextField()
        { (textField) in
            //this is for configuring the text field the user will see
            textField.borderStyle = UITextField.BorderStyle.bezel
        }

        alert.addAction(UIAlertAction(title: "OK", style: .default)
        { action in
            //use this space to transfer any data
            let textField = alert.textFields![0]
            self.someStringVariable = textField.text ?? ""
        })

        self.present(alert, animated: true)
        {
            //this will run once the action of **presenting** the view
            //is complete, rather than when the presented view has been
            //dismissed
        }
    }

    override viewDidLoad()
    {
        super.viewDidLoad()
        self.presentAnAlert()
        print(self.someStringVariable)
        //prints whatever the user input before pressing the OK button
    }
}

我希望这有帮助

UIAlertController,addTextFieldWithConfigurationHandler,UIAlertControllerStyleAlert,…您能详细说明一下吗?let alert=UIAlertControllertitle:添加位置,消息:编写快速描述,首选样式:UIAlertControllerStyle.alert alert.addActionUIAlertActiontitle:单击,样式:UIAlertActionStyle.Default,处理程序:nil alert.addTextFieldWithConfigurationHandler{textField:UITextField!在textField.placeholder=输入text:textField.securetextcentry=true}self.presentViewControlleralert,动画:true,完成:NilImplementHandler in addAction。点击按钮时会调用它。这是您的程序应该继续的位置。再次显示警报或继续错误输入。您不能在同一位置显示警报并等待,否则将阻止主进程读取-冻结。您必须在操作处理程序中处理输入。
import UIKit

class MyViewController: UIViewController
{
    var someStringVariable:String = ""

    func presentAnAlert()
    {
        let alert = UIAlertController(
                title: "Title",
                message: "Message",
                preferredStyle: .actionSheet //choose which style you prefer
        )

        alert.addTextField()
        { (textField) in
            //this is for configuring the text field the user will see
            textField.borderStyle = UITextField.BorderStyle.bezel
        }

        alert.addAction(UIAlertAction(title: "OK", style: .default)
        { action in
            //use this space to transfer any data
            let textField = alert.textFields![0]
            self.someStringVariable = textField.text ?? ""
        })

        self.present(alert, animated: true)
        {
            //this will run once the action of **presenting** the view
            //is complete, rather than when the presented view has been
            //dismissed
        }
    }

    override viewDidLoad()
    {
        super.viewDidLoad()
        self.presentAnAlert()
        print(self.someStringVariable)
        //prints whatever the user input before pressing the OK button
    }
}