ios Swift:使用按钮导航到新的 ViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27094923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift: Navigate to new ViewController using button
提问by DannieCoderBoi
I have a single view application which connects to parse and verifies user log in information and updates a UILabel saying "Yay, You've logged in sucessfully" - Now I want it to navigate to a new view (Which will be the main part of the app).
我有一个单视图应用程序,它连接到解析和验证用户登录信息并更新 UILabel 说“是的,您已成功登录” - 现在我希望它导航到一个新视图(这将是应用程序)。
I have dragged a new view controller onto the storyboard and Ctrl-Drag the button and linked it to the new controller with show. However, the moment I load the app and click the button it goes straight to the new view. I need it to only go there if the right part of the if-else statement is triggered.
我将一个新的视图控制器拖到情节提要上,然后按住 Ctrl 键拖动按钮并使用show将其链接到新控制器。但是,当我加载应用程序并单击按钮时,它会直接进入新视图。我需要它只在 if-else 语句的正确部分被触发时才去那里。
Does this make sense? Thank for any help guys. much appreciated.
这有意义吗?感谢任何帮助家伙。非常感激。
EDIT
编辑
The if statement is:
if 语句是:
if usrEntered != "" && pwdEntered != "" {
PFUser.logInWithUsernameInBackground(usrEntered, password:pwdEntered) {
(user: PFUser!, error: NSError!) -> Void in
if user != nil {
self.messageLabel.text = "You have logged in";
} else {
self.messageLabel.text = "You are not registered";
}
}
}
and its located in the ViewController.swift file
它位于 ViewController.swift 文件中
回答by Lyndsey Scott
First off as I explain in this answer, you need to drag the segue from the overall UIViewController to the next UIViewController, i.e. you shouldn't specifically connect the UIButton (or any IBOutlet for that matter) to the next UIViewController if the transition's conditional:
首先,正如我在此答案中所解释的那样,您需要将整个 UIViewController 中的 segue 拖到下一个 UIViewController,即如果转换是有条件的,则不应将 UIButton(或任何 IBOutlet)专门连接到下一个 UIViewController:
You'll also need to assign an identifier to the segue. To do so, you can select the segue arrow then type in an identifier within the right-hand panel:
您还需要为 segue 分配一个标识符。为此,您可以选择 segue 箭头,然后在右侧面板中输入标识符:
Then to perform the actual segue, use the performSegueWithIdentifier
function within your conditional, like so:
然后要执行实际的转场,请performSegueWithIdentifier
在条件中使用该函数,如下所示:
if user != nil {
self.messageLabel.text = "You have logged in";
self.performSegueWithIdentifier("segueIdentifier", sender: self)
} else {
self.messageLabel.text = "You are not registered";
}
where "segueIdentifier" is the identifier you've assigned to your segue within the storyboard.
其中“segueIdentifier”是您在故事板中分配给您的 segue 的标识符。