3

On my app's start up, it programmatically shows a LoginViewController using a segue. The view controller is presented modally with transition set to cross dissolve. Upon successful authentication, I want to dismiss the login view by programmatically triggering an unwind segue. So I added this to my header file:

- (IBAction)unwindSegue:(UIStoryboardSegue *)segue;

now in IB I'm able to control-drag from the "File's Owner" LoginViewController to the Exit button and choose unwindSegue:. This creates a manual segue, it shows up in the Connections inspectors for the File's Owner and the Exit button correctly. I then click on the newly created Unwind segue from the scene in IB and then give it a name. If I click on the "go to" button for the unwind segue action it takes me to the declaration mentioned above.

So far so good, I then trigger this unwind segue upon successful authentication in my GCD block:

....
dispatch_async(dispatch_get_main_queue(), ^
{
    [self performSegueWithIdentifier:@"UnwindSegueIdentifier" sender:self];

    [self.spinner removeFromSuperview];
    self.spinner = nil;
});

.....and nothing happens when it runs. The spinner does get removed correctly, but there's no sign of that unwind segue executing.

A break point in the implementation of unwindSegue: never gets hit. There are no errors thrown. Nothing gets written to the console. The identifier is correct, I triple checked (otherwise it will fail anyway).

I looked at the answers here, here and here but I don't seem to have missed anything.

What I did notice though, is that Xcode thinks unwindSegue: is not linked: linked? not linked?

I'm unable to drag from the little empty circle in front of unwindSegue: and link it to the Exit button.

Any help will be appreciated.

Community
  • 1
  • 1
Allen Zeng
  • 2,635
  • 2
  • 20
  • 31
  • Why don't you simply let the exit button trigger the segue, i.e., wire the button directly? – Matthias Feb 19 '13 at 13:51
  • Thanks for your reply but I think I might not have expressed myself clearly there... The Exit button I kept talking about was the green exit segue button, not an actual button on the view – Allen Zeng Feb 19 '13 at 22:03
  • What exactly should trigger your unwind segue? And why do you need to do it in an asynchronous manner, i.e. why do you want to use GCD? – Matthias Feb 20 '13 at 07:36
  • The segue is triggered upon successful authentication. The authentication is done by calling a web service. – Allen Zeng Feb 20 '13 at 09:13

2 Answers2

8

If you are using a modal segue to go to your login view, all you need to go back is to call

[self dismissViewControllerAnimated:YES completion:nil];

More precisely, you should call it at the presenting controller (your first controller), but it will be forwarded if you call at at the presented controller. You can use the completion block do any required clean up. There is no need to use GCD.

EDIT

To answer the additional comment: I'm not really sure from your description, but it seems you've implemented the unwind action at the presented controller instead at the presenting controller. Unwind segues are to allow to do something at the caller (e.g., setting data) without an additional protocol.

jackslash
  • 8,550
  • 45
  • 56
Matthias
  • 8,018
  • 2
  • 27
  • 53
  • 1
    Thanks. I understand that I can call `dismissViewControllerAnimated:completion:`, however I am more curious as to why the unwind segue doesn't work. I have just wired it up to a button but it still doesn't work. Also the use of GCD is so that the authentication web service call doesn't hold up the UI, not just to dismiss the login view controller. – Allen Zeng Feb 20 '13 at 10:01
  • You're totally right! I didn't implement the segue at the right place... I should have seen that earlier... Thank you! – Allen Zeng Feb 20 '13 at 12:55
  • Did notice that Xcode is still reporting the method to be disconnected as I have shown in the screenshot though. Must be a bug – Allen Zeng Feb 20 '13 at 12:59
2

Quoting text from Apple's Technical Note on Unwind Segue: To add an unwind segue that will only be triggered programmatically, control+drag from the scene's view controller icon to its exit icon, then select an unwind action for the new segue from the popup menu.

Link to the Technical Note

Vishal Chaudhry
  • 2,834
  • 1
  • 22
  • 8