简体   繁体   English

警报 iOS swift

[英]alert iOS swift

When I test for bad data in a text field and try to show an alert for missing or bad data the alert does not display.当我测试文本字段中的错误数据并尝试显示丢失或错误数据的警报时,警报不会显示。 But I can get an alert to display in the viewDidLoad function但是我可以在 viewDidLoad function 中显示一个警报

Here is the code这是代码

 import UIKit
 import CoreData

class SellViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate  {
 @IBOutlet weak var customer: UITextField!
@IBOutlet weak var bales: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    let alert = UIAlertController(title: "Hey", message: "@ SellViewController viewDidLoad ", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)

func checkDataInput(){

    print("checking data input customer.text  \(customer.text)")
    print("checking data input bales.text \(bales.text)")

    if (customer.text!.isEmpty) {
        customer.text = "REQUIRED"
        missingCustomer()
    }
if (bales.text!.isEmpty){
        availableAlert()
    }else{
        newQuantity = Int(bales.text!)!
     }

 func availableAlert() {
    print(" at availableAlert")

    let alert = UIAlertController(title: "Hey", message: "@ SellViewController func missingValues ", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)


func missingCustomer() {
    print(" at missingCustomer")
    let alert = UIAlertController(title: "Hey", message: "@ SellViewController func missingCustomer ", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
    }

This displays alert at viewDidLoad but no alert when I have missing data.这会在 viewDidLoad 处显示警报,但当我缺少数据时不会显示警报。 The print statements when I have missing data return as.当我缺少数据时,打印语句返回为。

checking data input customer.text Optional("")检查数据输入 customer.text Optional("")
checking data input bales.text Optional("")检查数据输入 bales.text Optional("")
at missingCustomer在失踪客户
at availableAlert在可用警报
fatal error: unexpectedly found nil while unwrapping an Optional value致命错误:在展开可选值时意外发现 nil

The fatal error happens because the user is not able to correct their response when they get the alert.发生致命错误是因为用户在收到警报时无法更正他们的响应。

What am I doing wrong?我究竟做错了什么? Shouldn't the alert display immediately when my missingData functions are triggered and the be able to correct their entries?当我的 missingData 函数被触发并且能够更正它们的条目时,警报不应该立即显示吗?

This sounds like a threading issue.这听起来像是一个线程问题。 Your data evaluation code is most likely executing on the main thread (you haven't posted it so cant say for sure) so when you try to do UI work, presenting an alert in this case, it will not execute.您的数据评估代码很可能在主线程上执行(您还没有发布它所以不能肯定地说)所以当您尝试进行 UI 工作时,在这种情况下会显示警报,它不会执行。 To fix this, try this code instead in your missingData function:要解决此问题,请在您的 missingData function 中尝试使用此代码:

let alert = UIAlertController(title: "Hey", message: "@ SellViewController viewDidLoad ", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil))
dispatch_async(dispatch_get_main_queue()) {
    self.presentViewController(alert, animated: true, completion: nil)
}
let alert = UIAlertController(title: "Hey", message: "@ SellViewController viewDidLoad ", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)

you can change the code to ->您可以将代码更改为 ->

 let alert = UIAlertController(title: "Hey", message: "@ SellViewController viewDidLoad ", preferredStyle: UIAlertControllerStyle.Alert)
 let alertButton = UIAlertAction(title: "Working!!", style: UIAlertAction.Style.default)
alert.addAction(alertButton)
present(alert, animated: true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM