首页 > 解决方案 > Swift4 - 带有文本字段的多个警报 - 完成处理程序

问题描述

我在我的开始屏幕(viewDidAppear)工作。在应用程序的开头,应该有一个带有一些通知消息的警报。这工作正常。通知处单击“确定”后,下一个警报应该会弹出一个文本字段。在此文本字段中,您必须输入一个双精度值。我需要这个双值来在应用程序内部设置很多东西,所以我需要这个值在函数之外用它来计算。例如,通知消息描述了应用程序的工作方式。您单击确定。现在下一个警报弹出文本字段。例如,它会询问您的身高。输入身高并单击确定后,身高值是不同计算的主要部分。现在的问题是,在我输入双精度值(例如高度)之前,我的应用程序使用零值。这些是代码片段。

var iNeedTheDataHere:String?
///Alerts
//Start
    let startController = UIAlertController(title: "Notice", message: "notice message ", preferredStyle: .alert)
    let inputController = UIAlertController(title: "Set input", message: "Please set the input", preferredStyle: .alert)

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

        // Start Alert
        startController.addAction(UIAlertAction(title: "OK", style: .default, handler:{(action:UIAlertAction!) in
            self.input()
        }))
        self.present(startController, animated: true)
    }

    func input() {
        inputController.addTextField()

        let submitAction = UIAlertAction(title: "Submit", style: .default) { [unowned ac] _ in
            let answer = inputController.textFields![0].text
            iNeedTheDataHere = answer
        }
        inputController.addAction(submitAction)
    }

当我设置断点时,在我输入任何内容之前,我看到 inputController 的值设置为 nil。我想我的错误与处理程序有关对不起我的英语希望有人可以帮助我

标签: swiftswift4handleralertscompletion

解决方案


    let alert = UIAlertController(title: "Alert!!!", message: "Please enter your message ", preferredStyle: .alert)
alert.addTextField(configurationHandler: self.configurationTextField)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:{ (UIAlertAction) in
 self.number.text?=""
 MBProgressHUD.hide(for: self.view, animated: true)
 }))
 alert.addAction(UIAlertAction(title: "Done", style: .default, handler:{ (UIAlertAction) in

    }))
    self.present(alert, animated: true, completion: {
                            })

推荐阅读