[英]Swift: Navigate to new ViewController using button
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 .我将一个新的视图控制器拖到情节提要上,然后按住 Ctrl 键拖动按钮并使用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.
我需要它只在 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 文件中
First off as I explain in this answer , you need to drag the segue from the overall UIViewController to the next UIViewController, ie 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.您还需要为 segue 分配一个标识符。 To do so, you can select the segue arrow then type in an identifier within the right-hand panel:
为此,您可以选择 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 的标识符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.