在Swift中使用shouldPerformSegueWithIdentifier()方法 [英] Using shouldPerformSegueWithIdentifier( ) method in Swift

查看:94
本文介绍了在Swift中使用shouldPerformSegueWithIdentifier()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用swift的shouldPerformSegueWithIdentifier()方法,但它接受2个参数。这些是(标识符:String!,sender:AnyObject)

I am trying to use swift's shouldPerformSegueWithIdentifier() method, but it accepts 2 arguments. These are (identifier: String!, sender:AnyObject)

我的主要目标是在我的故事板中按下登录按钮时执行代码,并取决于一系列检查返回TRUE或FALSE,具体取决于是否提供了正确的用户名和密码。所以这是我的问题:

My main goal is to execute the code when pressing a login button in my storyboard, and depending of a series of checks return TRUE or FALSE, depending if whether the correct username and password were provided. So here are my questions:


  1. 我应该使用什么作为标识符? Apple的文档说明标识符是一个标识触发的segue的字符串。所以假设我的segue有loginSegueProcess的名字。我如何在我的ViewController中使用它将它分配给我的UIView?问题是我在我的代码中声明了方法,它要求我指定两个参数(标识符和发送者)。我怎么能提供论据?

  1. What am I supposed to use as the identifier? Apple's documentation it explains that the identifier is a string that identifies the triggered segue. So suppose that my segue had the name of loginSegueProcess. How could I use it in my ViewController tat is assigned to my UIView? The thing is that I declare the method in my code and it requires me to specify both arguments (identifier & sender). How could I provide the arguments?

这种方法真的能满足我的需求吗?我的意思是,只要点击我的登录按钮,它确实会停止segue过渡,并且取决于是否提供了正确的凭据,它将带您到下一个视图,或者它将显示,例如,一个AlertView。 / p>

Will this method actually fulfill my needs? By that I mean if it will indeed stop the segue transition whenever my Login button is clicked and depending on whether the correct credentials were provided it is going to take you to the next View or it will show, say for example, an AlertView.

最后,我认为performSegueWithIdentifier(args)方法对我也有帮助。有人知道它们之间的区别吗?

Finally, I was thinking that the performSegueWithIdentifier(args) method would help me as well. Does anybody know the difference between them?

提前多多谢谢!

推荐答案

你可能不会调用 shouldPerformSegueWithIdentifier()自己的方法。它将在转换到下一个视图之前自动调用,从而有机会确定应该发生转换还是。您可以有条件地从此方法返回YES / NO。如果你的情况不涉及任何服务器调用,那么简单的逻辑检查这种方法就足够了。

You may not invoke shouldPerformSegueWithIdentifier() method by yourself. It will be automatically called just before transition to the next view giving a chance to determine wether the transition should take place or. You may conditionally return YES/NO from this method. If your condition does't involve any sever call,a simple logical checking this method will be enough for you.

performSegueWithIdentifier()用于以编程方式调用segue。考虑上面的网络调用情况,您可以从 shouldPerformSegueWithIdentifier()返回NO,因为身份验证正在进行。从服务器获得响应后,如果成功,您可以调用segue执行 performSegueWithIdentifier (此处标识符是您在故事板中给出的ID)。在确保你应该从 shouldPerformSegueWithIdentifier()返回YES之前。

performSegueWithIdentifier() is used to invoke a segue programmatically. Consider the above case with a network call, you may return NO from shouldPerformSegueWithIdentifier() initially since authentication is going on. After getting the response from server if it success you can call the segue to execute with performSegueWithIdentifier (Here the identifier is the ID you have given in the storyboard). Before make sure you are supposed to return YES from shouldPerformSegueWithIdentifier().

如果你的segue是从登录按钮连接的话,现在是第三种情况(你必须从控制器本身连接它)。不再需要检查 shouldPerformSegueWithIdentifier 。从服务器获取成功响应后,您可以使用 performSegueWithIdentifier()调用segue

Now a third case if your segue is connecting from the login button(You have to connect it from the controller itself). The checking of shouldPerformSegueWithIdentifier is no more required. You can just call the segue with performSegueWithIdentifier() after getting the success response from server

这篇关于在Swift中使用shouldPerformSegueWithIdentifier()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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