0

I'm trying to use a segue, here's my code:

if let status_code = response.response?.statusCode {
    if status_code == 200 {
        // register is ok
        // cancel all warnings
        self.usernameField.hidden = false;
        self.usernameFieldError.hidden = true;
        self.usernameShortError.hidden = true;
        self.usernameTakenError.hidden = true;

        self.performSegueWithIdentifier("registerToFeed", sender: self);
    }
}

(I've cut the code for length)
So here's the problem: the segue is simply not executed. I've put a breakpoint on it to see what happens, and the execution flow just passes on it, and do nothing, quite frustrating.

I've read that performSegueWithIdentifier needs to be called on the main thread, so I called NSThread.isMainThread()just before my segue call, and it returned me true.

I also double checked if the identifier name was the same as the one i'm calling and that's the case.

I tried to do

dispatch_async(dispatch_get_main_queue()){
    self.performSegueWithIdentifier("registerToFeed", sender: self)
}

as I've seen in another SO thread, but still not working, nothing gets executed.

So I don't know why this simple line doesn't get executed. Thank you for your help.

EDIT: so here's the full RegisterViewController.swift file:
http://pastebin.com/dpBBXhe8

The problem is that the program crashes at line 190 because the performSegue is not executed.

Community
  • 1
  • 1
mgul
  • 742
  • 8
  • 27
  • Just a tip: you do not need semicolons in swift! Check this out: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID316 – Pranav Wadhwa Jun 07 '16 at 21:39
  • 1
    i know! that's just because I don't want to lose the habitude, as I'm programming with other languages who need semicolons :) – mgul Jun 07 '16 at 21:52

2 Answers2

2

Is this code in the viewDidLoad method? If so, you must move it to viewDidAppear.

Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
0

Where was the viewController (self) in this code originated from. The performSegueWithIdentifier(_:sender:) requires the calling viewController to be loaded from a storyboard (storyboard must not be nil).

Also, have you tried implementing preapreForSegue to see if this is called when you call performSegue? The default implementation of 'prepareForSegue' does nothing.

DJohnson
  • 929
  • 4
  • 15
  • The calling viewController is loaded from a storyboard here. To create the segue, I've just ctrl-dragged the first view (called RegisterViewController) to the destination (FeedViewController) and then set the identifier. I tried to override the prepareForSegue function but still doesn't work, thank you for your answer though – mgul Jun 07 '16 at 21:28
  • As a test, try dragging a button to your storyboard, and create the segue from the button to the new viewController and see if that works? I've also seen storyboard files get messed up. Might want to check the XAML of the storyboard and see if there is anything funky around the segue or viewController (old identifier, incomplete markup, etc...) and try setting the sender to nil, and change the name of the seque identifier in the call and make sure it blows up (like it should). – DJohnson Jun 07 '16 at 21:31
  • Instead of adding another button, I've just tested my login view controller. It does the same thing: test fields, and `performSegueWithIdentifier`, so I tried it and it worked, I could get to the FeedViewController. – mgul Jun 07 '16 at 21:56
  • I've also tried with a new button: it works too `@IBAction func testSegue(sender: AnyObject) { performSegueWithIdentifier("registerToFeed", sender: nil); } `and to follow your answer, I tried to intentionnaly blow up the call by renaming the identifier, I had the correct error (no segue with such name) and turned it back, same thing. – mgul Jun 07 '16 at 21:58
  • Last couple suggestions. We can't see what else or where the above code executes from, but try moving the performSegue out of the if/then block, or moving to another area of code to make sure it's executing properly. Also, clean the code (remove derived data) and restart XCode. If none of this works, I'm happy to look at the project and see if I can find the issue if you want to do that. – DJohnson Jun 07 '16 at 23:46
  • if I put the performSegue out of the if/then, just after the `if let status_code = response.response?.statusCode {` statement, it works properly, but of course I can't do additional tests. So the problem is really when I'm the ìf status_code == 200 {`statement. Cleaning the code (Product > Clean) didn't solve the problem too. – mgul Jun 08 '16 at 11:24
  • 1
    That tells us something is wrong with the if statement code. Did you debug the statusCode response? – DJohnson Jun 08 '16 at 11:25
  • What do you mean by debug? I've put a breakpoint in it to see if the execution flow actually reads it and it does. The status_code (when my register fields are correct) is 200, so everything is fine in this side – mgul Jun 08 '16 at 11:27
  • I have added the RegisterViewController.swift file in my post – mgul Jun 08 '16 at 11:33