如何从 UIAlertcontroller 获取文本输入或如何使用 Swift 等待输入

How to get text input out of UIAlertcontroller OR how to wait for the input using Swift

我正在尝试提供一个 Alertcontroller,提示用户输入文件名,然后在程序的其他地方使用该文件名。我一直在尝试以下代码的多种变体:

import UIKit

class ViewController: UIViewController {

var shortName: String!

@IBAction func saveFile(sender: AnyObject) {

       //request filename with alert

    var alertController:UIAlertController?
    alertController = UIAlertController(title: "Enter File",
        message: "Enter file name below",
        preferredStyle: .Alert)

    alertController!.addTextFieldWithConfigurationHandler(
        {(textField: UITextField!) in
            textField.placeholder = ""
    })

    let action = UIAlertAction(title: "Submit",
        style: UIAlertActionStyle.Default,
        handler: {[weak self]
            (paramAction:UIAlertAction!) in
            if let textFields = alertController?.textFields{
                let theTextFields = textFields as [UITextField]
                let enteredText = theTextFields[0].text
                self!.shortName = enteredText      //trying to get text into shortName
                print(self!.shortName)             // prints
            }
        })

    alertController?.addAction(action)
    self.presentViewController(alertController!,
        animated: true,
        completion: nil)

 //do some stuff with the input

    print(shortName)  //THIS RETURNS nil if uncommented. comment out to avoid crash

}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

我已经对此进行了彻底的研究,但也找不到方法:

  1. 从 UIAlertAction 闭包中获取 shortName 的字符串值并放入 shortName(我知道当前的 "self!.shortName" 在闭包外不可用 - 不管我使用什么名称- 无法取出)

  2. 如果您 运行 程序 "as-is",print(shortName) 行将由于解包 nil 而导致崩溃。如何获得 "wait" 的输入提示?

大多数已发布的 "solutions" 都存在相同的问题 - 他们实际上并没有将文本输入从闭包中取出并放入程序其余部分可以访问的变量中。

谢谢

我认为是时候了。您试图在显示警报后立即打印 shortName。那个时候还没有定值。

您可以使用信号量等到它被设置,或者在 "submit".

的动作闭包中做任何您想做的事情

当然会崩溃,shortName 在未按下提交按钮时为零。你可以尝试这样的事情:

@IBAction func saveFile(sender: AnyObject) {

var alertController:UIAlertController?
alertController = UIAlertController(title: "Enter File",
    message: "Enter file name below",
    preferredStyle: .Alert)

alertController!.addTextFieldWithConfigurationHandler(
    {(textField: UITextField!) in
        textField.placeholder = ""
})

let action = UIAlertAction(title: "Submit",
    style: UIAlertActionStyle.Default,
    handler: {[weak self]
        (paramAction:UIAlertAction!) in
        if let textFields = alertController?.textFields{
            let theTextFields = textFields as [UITextField]
            let enteredText = theTextFields[0].text
            self!.shortName = enteredText      //trying to get text into shortName
            print(self!.shortName)             // prints

            self?.handleText()

            NSOperationQueue.mainQueue().addOperationWithBlock({
                self?.handleTextInMainThread()
            })
        }
    })

    alertController?.addAction(action)
    self.presentViewController(alertController!,
        animated: true,
        completion: nil)
}

func handleText() {
    print(self.shortName)
}

func handleTextInMainThread() {
    print(self.shortName)
}

如果您想在用户输入后在 handleTextInMainThread 中使用 UI,您必须使用 NSOperationQueue