[英]Present view controller not working on real device
我有一個應用程序,一旦登錄,它就會將用戶定向到另一個 viewController。
DispatchQueue.main.async {
let controller = HomeViewController()
controller.isLoggedIn = self.loggedIn
controller.userRole = self.userRole
controller.username = self.username
let navigationController = UINavigationController(rootViewController: controller)
if #available(iOS 13.0, *) {
navigationController.isModalInPresentation = true
navigationController.modalPresentationStyle = .overFullScreen
} else {
// Fallback on earlier versions
}
print("should present here")
self.present(navigationController, animated: true)
}
以上是“重定向”部分。 此代碼在模擬器中運行良好,但在真實設備上則無法運行。 知道我該如何解決嗎? 謝謝!
試試這個也設置主屏幕標識符 storyboard "HomeViewController"
在代碼中:
讓 controller = storyboard?.instantiateViewController(withIdentifier: "HomeViewController") 為! 主視圖控制器
使用 .fullScreen 作為 modalPresentationStyle 應該可以解決您的問題並提供在 iOS 12 或更低版本上使用的默認樣式。
if #available(iOS 13.0, *) {
navigationController.modalPresentationStyle = .fullScreen
} else {
// Fallback on earlier versions
}
您需要使用 storyBoard 參考來呈現 viewController。 您可以使用以下附加代碼的示例來執行此操作:-
let VC1 = self.storyboard.instantiateViewController(withIdentifier:"MyViewController") as! ViewController
`let navController = UINavigationController(rootViewController: VC1)` // Creating a navigation controller with VC1 at the root of the navigation stack.
self.present(navController, animated:true, completion: nil)
感謝您的回復,但我通過
self.dismiss(animated: true) {
self.present(navigationController, animated: true)
}
顯然它不起作用,因為有一個錯誤說有另一個視圖 controller 正在呈現(我之前錯過了這個錯誤)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.