2017-08-06 68 views
6

我不明白我的代碼有什麼問題。我只是用「Ok」按鈕顯示一個提醒,當用戶點擊「確定」時,然後提醒。但它並沒有消失。使用Swift3進行編程。 viewDidAppear()是否可以放這個代碼?或者我做錯了什麼?UIAlertController不會消失

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    let alertController = UIAlertController(title: "Wrong Item", message: "Could not find details of an item.", preferredStyle: .alert)   
    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) 
    present(alertController, animated: true, completion: nil) 
} 

UPDATE: 當我把在其他控制器相同的代碼,它的工作。 在原控制器中,在viewDidLoad(),我有一個Async調用如下。這是因爲這個問題嗎?

DispatchQueue.global(qos: .background).async { 
    self.getDetails(onCompletion: {(json: JSON) in 

    let dict = self.convertToDictionary(text: json.stringValue) 

     DispatchQueue.main.async { 

      self.activityIndicator.stopAnimating() 
      UIApplication.shared.endIgnoringInteractionEvents() 

      //Other UI update operation 

     } 
    }) 
}  

我也覆蓋viewWillDisappear()viewWillAppear(),只需設定屏幕的標題。

+0

你的代碼是好的,正確的,以及它完美的我 –

+0

奇怪的工作。這不適合我。 :( – NGR

+0

清理並運行一次 –

回答

1

viewDidAppear()添加下面的代碼之前警報代碼,並開始工作。我一直在下面的代碼在兩個位置,即在Async通話以及在viewDidAppear()

self.activityIndicator.stopAnimating() 
UIApplication.shared.endIgnoringInteractionEvents() 
-2

您的代碼看起來很好,而且viewDidAppear沒問題,因爲您的視圖控制器將被正確加載,並且不會破壞主線程以向您顯示警報。你的代碼應該存在一些其他問題,(Xcode的錯誤)或派生數據問題。

你可以做幾件事情要看到實際的問題:

  1. 清潔從導出的數據
  2. 刪除應用程序建立
  3. 刪除文件從模擬器
  4. 清潔再次
  5. 重新啓動Xcode和模擬器
  6. 重建以查看它是否有效。
+0

:(....仍然沒有工作 – NGR

+0

雖然這會有所幫助,它不是一個真正的答案是可能會更好過上幫助OP縮小問題的範圍問題的註釋。 – Abizern

+1

@Abizern,你是對的,但是我怎樣才能在格式正確的評論中寫出整個答案?我已經閱讀了很多其他答案,但仍然只包含一行建議以上任何內容,並且人們也接受或贊同他們。在這裏,因爲這個原因我收到了一個倒退。 – Hemang

5

你打電話UIApplication.shared.beginIgnoringInteractionEvents() 任何地方兄弟?如果是的,那是你的問題。

+0

是的。我已經在該控制器中確保用戶在加載時不能點擊UI。但是我也有UIApplication.shared.endIgnoringInteractionEvents()在viewDidLoad()中的異步調用中結束它。警報代碼在viewDidAppear() – NGR

+0

我也認爲這是問題,請參閱您在後臺使用隊列,因此它有一個低優先級來完成。這也將補充您的活動指示器在屏幕上時可以顯示警報的事實。 –

+0

我的建議是,(我假設你真的只想在加載時讀取你的json,並且希望在顯示屏幕時觸發警報,即使它在返回中也是如此)將呼叫置於特定功能中提醒並在endignoring和viewdidappear之後在viewdidload中調用它。創建一個變量作爲一個標誌來控制警報是否被viewdidload調用,因此不應該被viewdidappear調用。 –

0

endIgnoringInteractionEvents()當您單擊OK時,該方法處於異步方法,此方法尚未被調用。所以你不能解僱警報。

1

如果我們創建一個新的「單一視圖」項目 用於呈現警報有以下兩種方式,我們得到以下行爲

override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     let alertController = UIAlertController(title: "Wrong Item", message: "Could not find details of an item.", preferredStyle: .alert) 
     alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) 
     present(alertController, animated: true, completion: nil) 
    } 

在控制檯,你會看到

2017- 08-15 16:27:35.871測試[66719:7754741]警告:嘗試呈現其視圖不在窗口層次中的視圖!

並且在UI上沒有提示。

和:

override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 

     let alertController = UIAlertController(title: "Wrong Item", message: "Could not find details of an item.", preferredStyle: .alert) 
     alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) 
     present(alertController, animated: true, completion: nil) 
    } 

每一件事情按預期工作。

所以是的,視圖確實出現的地方。

關於viewDidLoad()viewDidAppear(_:)

從它的問題在於 beginIgnoringInteractionEvents

如果你把你的警報上viewDidAppear你應該看到它,但如果你不看長相它請注意以下幾點:

即使你把這段代碼放在viewdidload

 DispatchQueue.main.async { 

      self.activityIndicator.stopAnimating() 
      UIApplication.shared.endIgnoringInteractionEvents() 

      //Other UI update operation 

     } 

它可能會在以後執行(取決於當解析完成上)比viewDidAppear 呼叫,這是因爲:

DispatchQueue.global(QOS:系統技術領域) .async

你檢查這個原因?