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

时间:2018-03-13 08:33:05

标签: uiviewcontroller swift4

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

如何通过快速代码执行此操作。

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

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

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

3 个答案:

答案 0 :(得分:-1)

喜欢这个

keys

答案 1 :(得分:-1)

试试这个 -

@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 :(得分:-1)

试一下

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)**