![](/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.