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

时间:2016-01-28 17:21:46

标签: swift uialertcontroller textinput

我正在尝试提供一个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&#34 ;, print(shortName)行会因unil展开而导致崩溃。我怎样才能得到警告"等待"输入?

  3. 大多数发布的"解决方案"有同样的问题 - 他们实际上并没有从闭包中获取文本输入,而是进入一个可由程序其余部分访问的变量。

    感谢

2 个答案:

答案 0 :(得分:2)

当然你崩溃了,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)
}

如果您想在用户输入后使用NSOperationQueue内的用户界面,则可以使用handleTextInMainThread

答案 1 :(得分:0)

我认为这是时机。您尝试在显示警报后立即打印 shortName 。那时候的价值还没有确定。

您可以使用信号量等待它设置,或者在“提交”的操作闭包中执行您想要执行的任何操作。