![](/img/trans.png)
[英]Swift - How do I segue with a button from ViewController to a specific TabBarController
[英]How to move from Viewcontroller to TabBarController without segue
我的應用程序中有這樣的邏輯:
在服務器收到 200 個響應后,我想從 ViewController 移動到 HomeScreen,在任何其他情況下,它將以編程方式完成。 我知道可以附加到按下按鈕的 segue,但是當我使用 segue 並在字段中沒有文本的情況下按下登錄 btn 時,我也會移動到主屏幕,這就是為什么我認為在這種情況下不能使用 segue(我可以錯誤),所以我為主屏幕添加了 id 並添加了這樣的代碼來按下 btn:
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "homeScr") as? UITabBarController
self.navigationController?.pushViewController(vc!, animated: true)
當我按下這個按鈕時,注意到發生了。 我做錯了什么以及如何解決這個問題?
根據蘋果文檔,您推送的 controller 不能是 Tabbar 控制器。
The view controller to push onto the stack. This object cannot be a tab bar controller.
編輯:作為替代方案,這可能是一個解決方案:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "mainApp") as! UITabBarController
nextViewController.modalPresentationStyle = .fullScreen
self.present(nextViewController, animated:true, completion:nil)
在您的 appDelegate 中,添加屬性:
final var window: UIWindow?
在您的登錄視圖中像這樣打開 main controller
func openMain() {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier:
"mainApp") as! UITabBarController
let window = UIApplication.shared.delegate!.window!!
window.rootViewController = nil
window.rootViewController = nextViewController
UIView.transition(with: window, duration: 0.4, options: [.transitionCrossDissolve], animations: nil, completion: nil)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.