2016-08-12 78 views
-1

当我在文本字段中测试不良数据并尝试显示缺失或不良数据警报时,不显示警报。但是,我可以得到一个警报在viewDidLoad中功能提醒iOS swift

这里显示的是代码

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) 
    } 

这在viewDidLoad中显示警报,但当时我已经丢失的数据没有任何警报。我缺少数据时的打印语句返回为。

检查数据输入customer.text可选( “”)
检查数据输入bales.text可选( “”)
在missingCustomer
在availableAlert
致命错误:意外地发现零而展开的可选值

发生致命错误的原因是用户在得到警报时无法纠正其响应。

我在做什么错?当我的 missingData函数被触发并且能够更正它们的条目时,不应该立即显示警报吗?

+1

为什么不发布相关代码,因为这是您的问题所在? – rmaddy

+0

在主线程上执行'missingData'函数吗?如果没有,请使用'DispatchQueue.main.async {/ *在这里显示您的提醒* /}'(或'dispatch_async(dispatch_get_main_queue()){/ *在Swift 2.0中显示您的提醒* /}') – Palle

回答

0

这听起来像是一个线程问题。您的数据评估代码很可能在主线程上执行(您尚未发布,因此无法确定),因此当您尝试执行UI工作时,在此情况下显示警报,它将不会执行。要解决此问题,请尝试使用此代码,而不是在您的missingData函数中:

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)) 
dispatch_async(dispatch_get_main_queue()) { 
    self.presentViewController(alert, animated: true, completion: nil) 
} 
+0

我试过了您的建议。不幸的是我得到了同样的结果。我编辑我的帖子,包括相关的代码,视图控制器和几个函数。我一定在做别的事情。 –