简体   繁体   English

iOS:让segue等待登录成功

[英]iOS: Make segue wait for login success

I am designing an iOS app using XCode 4.2's storyboard feature. 我正在使用XCode 4.2的故事板功能设计一个iOS应用程序。 The app has a login screen that takes a username and password and has a button to log in. Pushing the login button triggers a push seque to another view controller. 该应用程序有一个登录屏幕,其中包含用户名和密码,并有一个登录按钮。按下登录按钮会触发另一个视图控制器的推送序列。 However, I want the seque to wait until the login comes back successful before proceeding to the next view controller. 但是,我希望seque等到登录成功后再继续下一个视图控制器。

I know about prepareForSegue:sender: but heres my issue: the login call is asynchronous. 我知道prepareForSegue:sender:但是我的问题是:登录调用是异步的。 I therefore cannot perform the login there. 因此我无法在那里执行登录。

Is there someway around this? 有什么事吗? Can I create a seque in the storyboard that is only triggered when I want it to be (as opposed to when a button is clicked)? 我可以在故事板中创建一个仅在我想要时触发的seque(而不是单击按钮时)?

Ok, I have figured it out. 好的,我已经弄清楚了。 I had defined a segue from a UIButton to the next UIViewController . 我已经定义了一个从UIButton到下一个UIViewController的segue。 As defined in this manner, there is no way to conditionally execute the segue. 如以这种方式定义的,没有办法有条件地执行segue。

Instead of putting the segue on the button, I made a segue from the first UIViewController to the other UIViewController . 我没有把segue放在按钮上,而是从第一个UIViewController到另一个UIViewController做了一个segue。 This defines the segue, but associates no action with it. 这定义了segue,但不与它相关联。 From there I can simply call performSegueWithIdentifier:sender: when I want that segue to execute (in my case being when the async task is complete). 从那里我可以简单地调用performSegueWithIdentifier:sender:当我想要执行segue时(在我的情况下是异步任务完成时)。

The main advantage of this for me is that IB maintains its structure. 这对我来说的主要优点是IB保持其结构。

Big thanks to @cli_hlt for pointing me in the right direction (+1). 非常感谢@cli_hlt指出我正确的方向(+1)。

I don't know if there is a way in IB to perform such an action (although I strongly doubt it) so I suggest to use 我不知道IB是否有办法执行这样的行动(尽管我强烈怀疑),所以我建议使用

- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender

on the view controller when your asynchronous login signals success. 当您的异步登录信号成功时,在视图控制器上。

You can also call instantiateViewControllerWithIdentifier on an instance of UIStoryBoard. 您还可以在UIStoryBoard的实例上调用instantiateViewControllerWithIdentifier。 This will return a view controller reference to any point in your storyboard. 这将返回一个视图控制器引用到故事板中的任何点。 From there, you can present it however you like and it will pick up the flow of your storyboard. 从那里,您可以随心所欲地呈现它,它将获得故事板的流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM