Swift:使用按钮导航到新的 ViewController [英] Swift: Navigate to new ViewController using button

查看:17
本文介绍了Swift:使用按钮导航到新的 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单视图应用程序,它连接到解析和验证用户登录信息并更新 UILabel 说是的,您已成功登录" - 现在我希望它导航到一个新视图(这将是应用程序的主要部分).

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 键拖动按钮并使用 show 将其链接到新控制器.但是,当我加载应用程序并单击按钮时,它会直接进入新视图.我需要它只在 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

推荐答案

首先,正如我在 这个答案 中所解释的那样,您需要将整个 UIViewController 中的 segue 拖到下一个 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 箭头,然后在右侧面板中输入标识符:

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:

然后要执行实际的转场,请在条件中使用 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 的标识符.

where "segueIdentifier" is the identifier you've assigned to your segue within the storyboard.

这篇关于Swift:使用按钮导航到新的 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆