swift - 从 uiAlertController 中的文本字段检索内容

标签 swift textfield uialertcontroller

我在 uiAlertController 中有一个文本字段,我想检索其中的内容来构建一个对象。我写了以下代码:

let alertController = UIAlertController(title: "Ajouter une description", message: "De quoi sagit il ?", preferredStyle: UIAlertControllerStyle.alert)
                    alertController.addTextField { (textField : UITextField) -> Void in
                        textField.placeholder = "Description"
                        self.theDescription = textField.text!
                    }
                    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
                        self.createArtWithDescription(description: self.theDescription)
                    }
                    
                    alertController.addAction(okAction)
                    self.present(alertController, animated: true, completion: nil)
                    //
                }

为了构建我的对象,我有另一个函数“createArtWithDescription”,但在内部我无法检索 uiAlertController 文本字段的内容。

提前致谢

最佳答案

简短回答:

    let okAction = UIAlertAction(title: "OK", style: .default) { (result : UIAlertAction) -> () in
        self.theDescription = alertController.textFields?.first?.text
        self.createArtWithDescription(description: self.theDescription)
    }

长答案:

创建简单的 Playground 用于测试并在执行期间检查所有内容:

import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {

    private var theDescription:String?
    private var label:UILabel = UILabel()

    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
        label.text = "Hello World!"
        label.textColor = .black
        label.backgroundColor = .red

        view.addSubview(label)
        self.view = view
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        let alertController = UIAlertController(title: "Ajouter une description", message: "De quoi sagit il ?", preferredStyle:.alert)
        alertController.addTextField { (textField : UITextField) -> () in
            textField.placeholder = "Description"
            // you cant obtain value here - this block only for config purpose
            self.theDescription = textField.text
        }
        let okAction = UIAlertAction(title: "OK", style: .default) { (result : UIAlertAction) -> () in
            // instead use this approach
            self.theDescription = alertController.textFields?.first?.text
            self.createArtWithDescription(description: self.theDescription)
        }

        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
        //
    }

    func createArtWithDescription(description:String?) {
        DispatchQueue.main.async {
            self.label.text = description
        }
    }

}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

更多关于UIAlertController

关于swift - 从 uiAlertController 中的文本字段检索内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47110552/

相关文章:

ios - 无法选择 UIAlertController 中的按钮顺序

ios - 处理可观察序列 RxSwift 上的循环样式事件

ios - Swift: UIImagePicker 可以自动打开上次拍摄的照片吗?

java - 使用 RegEx 验证字符串

ios - 在按钮单击上生成文本字段 swift

swift - UIAlertAction 背景颜色

ios - UIAlertcontroller 中的可点击 url

swift - 如何从特定索引处的字典中删除对 - Swift?

json - 执行 segue 的 Swift 登录屏幕

javascript - 根据在文本字段中输入的值更改 Img Src