2016-08-12 76 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

我試過了您的建議。不幸的是我得到了同樣的結果。我編輯我的帖子,包括相關的代碼,視圖控制器和幾個函數。我一定在做別的事情。 –