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

标签 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. 如果您“按原样”运行程序,print(shortName) 行将由于 nil 的展开而导致崩溃。如何获得“等待”输入的警报?

大多数已发布的“解决方案”都有相同的问题 - 它们实际上并没有将文本输入从闭包中取出并放入一个可以被程序的其余部分访问的变量中。

谢谢

最佳答案

当然你会崩溃,shortName 是 nil 而没有按下提交按钮。你可以尝试这样的事情:

@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

关于swift - 如何从 UIAlertcontroller 获取文本输入或如何使用 Swift 等待输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35067931/

相关文章:

ios - 在 SpriteKit 中设置布局约束

ios - UIAlertController 中的自定义 View

JSF inputText 和 inputSecret 大小不同

keyboard - flutter 数字键盘未完成按钮

java - 计算文件中的字符数

快速添加可点击的表格 View

ios - 使用 UIGraphicsPDFRenderer 创建 PDF 时添加像 "page x of y"这样的页脚

arrays - 从 Swift 中的数组中获取 3 个独特的项目 - 每个项目都有独特的值(value)

ios - 在 iPhone 上的弹出窗口中呈现 UIAlertController ActionSheet

ios - 如何将 uialertcontroller 文本字段键盘更改为数字键盘?