代码之家  ›  专栏  ›  技术社区  ›  BT.ios

以编程方式打开视图控制器场景(Swift 4)

  •  0
  • BT.ios  · 技术社区  · 7 年前

    我想创建一个有两个按钮的基本iOS应用程序。轻触第一个按钮以显示视图控制器1,轻触第二个按钮以显示视图控制器2。

    我怎样才能通过swift代码做到这一点。

    我将第一个按钮连接到根视图控制器上的以下代码:

    @IBAction func showViewOneTapped(_ sender: Any) {
        // this is where I need help I think
    }
    

    我还为我希望显示的每个视图控制器创建了shell swift文件。

    3 回复  |  直到 7 年前
        1
  •  0
  •   Rashed    7 年前

    试试这个-

    @IBAction func showViewOneTapped(_ sender: Any) {
            let storyBoard : UIStoryboard = UIStoryboard(name: "YourStoryBoardName", bundle:nil)
            let nextViewController = storyBoard.instantiateViewController(withIdentifier: "YourIdentifierName") as! yourClassname(e.g PhoneNoDropDownViewController)
            self.present(nextViewController, animated:true, completion:nil)
        }
    
    
    @IBAction func showViewTwoTapped(_ sender: Any) {
                let storyBoard : UIStoryboard = UIStoryboard(name: "YourStoryBoardName", bundle:nil)
                let nextViewController = storyBoard.instantiateViewController(withIdentifier: "YourIdentifierName") as! yourClassname(e.g PhoneNoDropDownViewController)
                self.present(nextViewController, animated:true, completion:nil)
            }
    
        2
  •  0
  •   Gökhan Koca    7 年前

    像这样

    let vc1 = self.storyboard?.instantiateViewController(withIdentifier: "ViewController1Identifier") as! ViewController1
    self.present(vc1, animated: true, completion: nil)
    
        3
  •  0
  •   Rafiqul Hasan    7 年前

    试试这个

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let nextViewController = storyBoard.instantiateViewController(withIdentifier: "Login") as! LoginViewController
        self.present(nextViewController, animated:true, completion:nil)
    

    **How to add Storyboard id (withIdentifier)**