[英]Swift tried to present modally an active controller [Current view controller] even though the presented controller is another one
我已經讀過一些答案,例如解雇當前的ViewController,但是我的情況有所不同,因為我要介紹另一個ViewController。
這段代碼向視圖控制器提供了其導航控制器,盡管我無法訪問它的屬性:
@IBAction func didTouchAddFriend(_ sender: UIButton) {
DispatchQueue.main.async {
let storyBoard = UIStoryboard(name: "CustomContact", bundle: nil)
let customContactVC = storyBoard.instantiateViewController(withIdentifier: "CustomContact")
}
}
雖然這不起作用
@IBAction func didTouchAddFriend(_ sender: UIButton) {
DispatchQueue.main.async {
let navigation = UIStoryboard.init(name: "CustomContact", bundle: nil).instantiateInitialViewController() as! UINavigationController
let pickerUserVC = navigation.viewControllers.first as! CustomContactsViewController
self.present(pickerUserVC, animated: true, completion: nil)
}
}
我還有一個名為CustomContact.storyboard的故事板,其中包含一個CustomContactsViewController
當我單擊didTouchAddFriend時,它僅顯示以下錯誤:
'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <Main.MainViewController: 0x7ff29f06ac00>.'
當我打印pickerUserVC的值時,我無法理解錯誤cos,它顯示了正確的視圖控制器:
<Main.CustomContactsViewController: 0x7fd1b7922400>
MainViewController是實現didTouchAddFriend函數的當前視圖控制器
您應該展示導航控制器 (它將CustomContactsViewController
托管為根視圖控制器)。
而且,您可以跳過Dispatch調用,因為我們位於已經在主線程中運行的@IBAction中:
@IBAction func didTouchAddFriend(_ sender: UIButton) {
let navigation = UIStoryboard.init(name: "CustomContact", bundle: nil).instantiateInitialViewController() as! UINavigationController
let pickerUserVC = navigation.viewControllers.first as! CustomContactsViewController
self.present(navigation, animated: true, completion: nil)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.