swift2 等待,直到用户完成对Swift中弹出对话框的响应

mpbci0fu  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(187)

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

u3r8eeie

u3r8eeie1#

使用UIAlertController

let alertController = UIAlertController(title: "title", message: nil, preferredStyle: .Alert)
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in }
alertController.addAction(UIAlertAction(title: "cancel", style: UIAlertActionStyle.Cancel, handler: nil))
logInAlertController.addAction(UIAlertAction(title: "go", style: UIAlertActionStyle.Default, handler: { (action) -> Void in }
rks48beu

rks48beu2#

雨燕四号
这听起来像是在呈现控制器而不是在用户选择警报操作时进行完成处理。
您可以尝试以下操作:

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

我希望这对你有帮助!

相关问题