2015-11-14 60 views
0

稱爲調用UIAlert警告時UIAlertController:0x7ff211650140斯威夫特

當試圖同時它 解除分配不允許加載視圖控制器的觀點,並可能導致不確定的行爲 (我得到這樣的警告有時)

code used: 

    func alert (dictKey: String){ 
     print(dictKey) 
     let alertController = UIAlertController(title: nil, message: promptsArr[dictKey], preferredStyle: .Alert) 
     let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in 

     } 
     alertController.addAction(OKAction) 
     if self.presentedViewController == nil { 
      delay(1.0){ 
       self.presentViewController(alertController, animated: true, completion: nil) 
      } 
     } else { 
      self.presentedViewController! 
     } 
+0

在哪裏以及如何ü調用此方法? – Nishant

+0

從這個開關代碼內: FUNC resultOfValidation(dictKey:字符串,標記的:int){ \t \t \t 延遲\t(0.3){ \t \t \t開關dictKey { \t \t \t案 「SendVeriCode」://短信發送 \t \t \t \t //服務器代碼都在不同名稱的搜索細胞的前一個條目 \t \t \t \t _ = self.alert(dictKey) \t \t \t \t如果(標記<6){self.VerifyInput [標籤+ 2] .becomeFirstResponder()} \t \t \t情況下 「取消」: \t \t \t \t _ = self.alert(dictKey) \t \t \t \t self.VerifyInput [標籤 - 1]的.text = 「」 \t \t \t \t self.VerifyInput [標籤]的.text = 「」 \t \t \t \t self.VerifyInput [tag - 2] .becomeFirstResponder() –

+0

請將該代碼添加到問題中。 – Nishant

回答

2
  1. 刪除delay()

  2. 把邏輯裏面self.presentedViewController(無需創建警報如果存在的話)

    func alert (dictKey: String){ 
        if self.presentedViewController == nil { 
         let alertController = UIAlertController(
          title: nil, 
          message: promptsArr[dictKey], 
          preferredStyle: .Alert) 
         let OKAction = UIAlertAction(title: "OK", style: .Default) { 
          (action) in 
         } 
         alertController.addAction(OKAction) 
    
         self.presentViewController(
          alertController, 
          animated: true, 
          completion: nil) 
        } 
    } 
    
+0

謝謝你會馬上嘗試。 –

+0

非常感謝。延遲(1.0){}被稱爲非常有用的同步兩個同時運行的進程的方法,也可以用來代替代碼中的確定,具有特定的時間延遲以清除警報 - 參考/ *由Matt創建的一小段代碼Neuburg,許多書籍的作者,包括 \t \t Swift的iOS編程基礎(O'Reilly 2015)。 \t \t看到他在堆棧溢出的細節回覆: \t \t http://stackoverflow.com/questions/24034544/dispatch-after-gcd-in-swift/24318861#24318861 \t */ 在那裏我得到這從 –

+0

http:// stackoverflow。com/questions/24034544/dispatch-after-gcd-in-swift/24318861#24318861與您可能需要的地方非常不同的情況。 「延遲」在任何情況下都不是可靠的同步方法。查看'-performSelector:afterDelay',信號量或(如果可能的話)簡單的完成塊。 – SwiftArchitect