[英]How can I switch programmatically between view controllers that are not on the same storyboard?
I am trying to switch between a view controller (that is coded programmatically) to a ViewController
with the UI Elements on the storyboard.我正在尝试在视图控制器(以编程方式编码)与故事板上具有 UI 元素的
ViewController
之间切换。
I am trying to switch between a HomeController
to a TabBarController
using a button but by pressing the button it switches to a dark screen.我正在尝试使用按钮在
HomeController
和TabBarController
之间切换,但通过按下按钮,它会切换到黑屏。 I would be glad if some of you could help me out.如果你们中的一些人能帮助我,我会很高兴。
Here is my code:这是我的代码:
var welcomeLabel: UIButton = {
let label = UIButton()
label.tintColor = .white
label.translatesAutoresizingMaskIntoConstraints = false
label.alpha = 0
label.addTarget(self, action: #selector(handleLogin), for: .touchUpInside)
return label
}()
...............
@objc func handleLogin() {
navigationController?.show(TabBarController(), sender: Any?.self)
}
如果您从NavigationController
推送视图,请使用此代码
navigationController?.pushViewController("YourViewController", animated: true)
You can do it in following way您可以通过以下方式进行
let storyboard = UIStoryboard(name: "Your_storyboard", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "Your_View_controller") as! Your_View_controller
Now to present现在介绍
self.present(viewController, animated: true, completion: nil)
Or through navigation controller或者通过导航控制器
navigationController?.pushViewController(viewController, animated: true)
You can try this -你可以试试这个——
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) // Give the name .e.g if you have the different story board name
let YourNextViewController = storyBoard.instantiateViewController(withIdentifier: "your story board identifier") as! StoryBoardName/ClassName
self.navigationController?.pushViewController(YourNextViewController, animated: true)
//Or if you want to present viewController
self.present(YourNextViewController, animated:true, completion:nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.