简体   繁体   English

如何在不在同一个故事板上的视图控制器之间以编程方式切换?

[英]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.我正在尝试使用按钮在HomeControllerTabBarController之间切换,但通过按下按钮,它会切换到黑屏。 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.

相关问题 如何在同一ViewController(在情节提要板上创建)的两个视图(在xib中创建)之间切换? - How can I switch between two view (created on xib) over the same ViewController (created on storyboard)? 如何正确在Storyboard中的视图控制器之间进行手动切换(是iOS)? - How properly manually to switch between View Controllers in Storyboard (is iOS)? 如何在Swift上的两个子视图控制器之间切换? - How can I switch between two child View Controllers on Swift? 如何在XCode中预览情节提要的不同视图控制器 - How can I preview different view controllers of a storyboard in XCode 如何以编程方式创建segue以在视图控制器之间传递信息 - Swift - How can I create a segue programmatically to pass info between view controllers - Swift 如何在视图控制器之间滑动? - How can I swipe between view controllers? 在视图控制器之间切换 - Switch between view controllers 如何使用Storyboard在iOS中的UIViewControllers之间切换 - How can I switch between UIViewControllers in iOS using Storyboard 在同一故事板上的2个视图控制器之间切换,使用willAutorotateTo轮换...不工作, - Switching between 2 view controllers on same storyboard, on rotation using willAutorotateTo… not working, 如何在故事板中手动切换UIViewControllers? - How can I manually switch between UIViewControllers in storyboard?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM