简体   繁体   English

Swift:使用按钮导航到新的 ViewController

[英]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.

相关问题 如何快速单击弹出式ViewController上的按钮单击导航到TabBar ViewController? - How to navigate to TabBar ViewController on click of button on popover ViewController in swift? 登录后自动将用户导航到Swift 3.0中的新ViewController - Automatically Navigate a User to a new ViewController in Swift 3.0 after Login 当以编程方式创建的按钮快速单击时导航到 viewController - Navigate to viewController when programmatically created button click in swift Swift 从第三个 ViewController 导航到第一个 ViewController - Swift Navigate from third ViewController to the first ViewController 在使用Swift展示新的Viewcontroller后如何关闭以前的Viewcontroller? - How to dismiss previous Viewcontroller after presenting new Viewcontroller using Swift? 从popoverPresentationController导航到新的ViewController - Navigate from popoverPresentationController to new ViewController 使用swift2从登录/注册ViewController导航到SWRevealViewController的sw_front ViewController - Navigate from Login/Signup ViewController to SWRevealViewController's sw_front ViewController using swift2 单击模态ViewController上的按钮,然后转到新页面swift 4 - click button on modal ViewController then go to new page swift 4 Swift:以编程方式导航到ViewController并传递数据 - Swift: Programmatically Navigate to ViewController and Pass Data 快速从子类功能导航到ViewController - Navigate to viewcontroller from a subclass function in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM