Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Make segue wait for login success

I am designing an iOS app using XCode 4.2's storyboard feature. 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.

I know about prepareForSegue:sender: but heres my issue: the login call is asynchronous. 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)?

like image 314
mtmurdock Avatar asked Nov 19 '11 00:11

mtmurdock


3 Answers

Ok, I have figured it out. I had defined a segue from a UIButton to the next UIViewController. As defined in this manner, there is no way to conditionally execute the segue.

Instead of putting the segue on the button, I made a segue from the first UIViewController to the other UIViewController. This defines the segue, but associates no action with it. 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).

The main advantage of this for me is that IB maintains its structure.

Big thanks to @cli_hlt for pointing me in the right direction (+1).

like image 145
mtmurdock Avatar answered Nov 04 '22 22:11

mtmurdock


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

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

on the view controller when your asynchronous login signals success.

like image 33
cli_hlt Avatar answered Nov 04 '22 21:11

cli_hlt


You can also call instantiateViewControllerWithIdentifier on an instance of UIStoryBoard. 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.

like image 1
Scott Ahten Avatar answered Nov 04 '22 22:11

Scott Ahten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!