当我按下按钮时,如何显示另一个(更改)ViewController

时间:2015-02-27 08:27:06

标签: xcode macos swift viewcontroller

在我的xcode故事板中,我有一个标准的viewController,我在其中放置了一个带有Action Outlet的Button。

我尝试使用无法加载的.xib。此外,我尝试在Storyboard中使用第二个ViewController和一个自定义segue,但这也失败了。

我做错了。请有人帮帮我吗?

2 个答案:

答案 0 :(得分:0)

如果您正在使用Segues,则无需创建ViewController并尝试以这种方式显示它。只需给你的ViewController一个(unqiue)名称(例如,如果你不想使用“NavigationController”,你应该使用“Modal”作为Type。你应该在Storyboard中为每个ViewController添加一个ViewController类。

因此,请将ViewController1与第二个View Controller连接,并为其命名为“loginModal”。

然后在您的IBAction中,您可以使用:

self.performSegueWithIdentifier("loginModal", sender: self)

应该是全部。如果要在ViewControllers之间发送数据,则应使用prepareForSegue。在这里查看我的答案,你会发现有关使用segues发送数据的更多信息。

Sending data with Segue with Swift

答案 1 :(得分:0)

你可以试试这个

将此代码放入@IBAction按钮。

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let loginViewController = storyBoard.instantiateViewControllerWithIdentifier("LoginView") as loginViewController
        self.presentViewController(loginViewController, animated:true, completion:nil)

不要忘记在loginViewController中设置标识符的名称。

enter image description here