swift - 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。我想我的错误与处理程序有关对不起我的英语希望有人可以帮助我
解决方案
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: {
})
推荐阅读
- ios - UIPickerView 作为 UITextView swift 的 inputView
- php - 跨多个表的 Laravel 关系失败
- python - Django 通过电子邮件从模型中发送用户数据
- wordpress - 如何在wordpress中使用rest api生成多个不同大小的图像
- php - Selenium PHP Web 驱动程序(Facebook)问题
- java - Spring Boot JPA(Hibernate) 如何保存图像
- selenium - 使用 Selenium Webdriver 提供登录详细信息不正确,即使其正确
- .net - 使用 .net 4.5.2 和 angularjs 在后台下载文件
- c# - 在无排序的未排序双(小数)数组中查找 n 个最大值
- javascript - 根据 jquery handlebar.js 的值禁用复选框