如何在不在同一故事板上的视图控制器之间进行编程切换?

时间:2019-06-30 04:17:49

标签: ios swift

我正在尝试使用情节提要上的UI元素在以编程方式编码的视图控制器和ViewController之间切换。

我正在尝试使用按钮在HomeControllerTabBarController之间切换,但是通过按下按钮,它会切换到黑屏。如果有人能帮助我,我会感到很高兴。

这是我的代码:

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)

}

3 个答案:

答案 0 :(得分:0)

如果要从NavigationController推送视图,请使用此代码

navigationController?.pushViewController("YourViewController", animated: true)

答案 1 :(得分:0)

您可以按照以下方式进行操作

let storyboard = UIStoryboard(name: "Your_storyboard", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "Your_View_controller") as! Your_View_controller

现在要呈现

self.present(viewController, animated: true, completion: nil)

或通过导航控制器

navigationController?.pushViewController(viewController, animated: true)

答案 2 :(得分:0)

您可以尝试-

    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)