Swift:使用按钮导航到新的ViewController [英] Swift: Navigate to new ViewController using button
问题描述
我有一个单独的视图应用程序,它连接到解析并验证用户登录信息并更新UILabel说Yay,你已成功登录 - 现在我希望它导航到一个新视图(这将是应用程序的主要部分)。
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).
我已将新的视图控制器拖到故事板上,然后按住Ctrl键并拖动按钮并使用显示将其链接到新控制器。但是,当我加载应用程序并单击按钮时,它会直接进入新视图。如果if-else语句的正确部分被触发,我只需要去那里。
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.
这有意义吗?感谢任何帮助的人。非常感激。
Does this make sense? Thank for any help guys. much appreciated.
编辑
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";
}
}
}
及其位于ViewController中.swift文件
and its located in the ViewController.swift file
推荐答案
首先我在这个答案,您需要将segue从整个UIViewController拖到下一个UIViewController,即如果转换的话,您不应该将UIButton(或任何IBOutlet)专门连接到下一个UIViewController。条件:
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:
您还需要为segue分配一个标识符。为此,您可以选择segue箭头,然后在右侧面板中键入标识符:
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,使用 performSegueWithIdentifier
在条件中的函数,如下所示:
Then to perform the actual segue, use the performSegueWithIdentifier
function within your conditional, like so:
if user != nil {
self.messageLabel.text = "You have logged in";
self.performSegueWithIdentifier("segueIdentifier", sender: self)
} else {
self.messageLabel.text = "You are not registered";
}
其中segueIdentifier是您在segue中分配给segue的标识符故事板。
where "segueIdentifier" is the identifier you've assigned to your segue within the storyboard.
这篇关于Swift:使用按钮导航到新的ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!