0

I have a login screen and when a button is pressed this code executes:

- (IBAction)btnLogin:(id)sender {
    [username resignFirstResponder];
    [password resignFirstResponder];


    Auth *authRequest = [[Auth alloc] init];



    NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:APIHOST path:@"/authenticate"];
    [authRequest setUrl:url];
    [authRequest setUsername:[username text]];
    [authRequest setPassword:[password text]];
    [authRequest authenticate];

    [self performSegueWithIdentifier:@"Login2Tab" sender:self];


}

This works well and the segue executes as expected (So I know the segue exists and its identified correctly in the storyboard). I however don't want to perform the segue until the connection has finished loading so this implementation file is my delegate for NSURLConnection and at the bottom I have (and the if/else on responseCode does work)..:

 // Close the connection
 - (void)connectionDidFinishLoading:(NSURLConnection*)connection {
     if(responseCode == 200) {
         [self performSegueWithIdentifier:@"Login2Tab" sender:self];
     } else {
         NSLog(@"Bad.. bad.. bad...");
     }     
     NSLog(@"Connection Closed."); 
 }

(When I put the performSegueWithIdentifer in the connectionDidFinishLoading I comment out the performSegueWithIdentier from above...).

But now the app always crashes stating:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (LoginViewController: 0x7163640) has no segue with identifier 'Login2Tab''

Which is where I am stuck because it works when its not called from within the connectionDidFinishLoading...

nwkeeley
  • 1,397
  • 5
  • 18
  • 28
  • 1
    Are the two methods (`btnLogin:` and `connectionDidFinishLoading:`) implemented in the same class (`LoginViewController`) ? – Guillaume Algis Jun 27 '13 at 13:56
  • Have you tried the suggestions from [this post](http://stackoverflow.com/questions/11874200/nsinvalidargumentexception-receiver-has-no-segue-with-identifier)? – Corey Jun 27 '13 at 14:00
  • @Guillaume yes they both are implemented in LoginViewController.m – nwkeeley Jun 27 '13 at 14:12
  • @Corey, just tried everything in that post.... no luck. Drives me crazy because it works when not called from that connectionDidFinishLoading... so I know its setup right just not sure why it can't see the segue from a delegate method... – nwkeeley Jun 27 '13 at 14:20
  • are you sure that the segue exists on both iPhone and iPad storyboards? potentially you are using a different one in which the segue doesn't exist? – THE_DOM Jun 27 '13 at 15:29
  • @THE_DOM I only have 1 storyboard... that segue code does not work in any of the delegate methods.. only in the IBAction of the button. So I know the Segue is there and working.. just can't call it from a delegate method. – nwkeeley Jun 27 '13 at 17:43
  • ok, try deleting the segue and recreating it. I have had some funky issues resolved with storyboards simply by redoing what was already there. I know it probably won't work, but it is worth a shot. – THE_DOM Jun 27 '13 at 19:12
  • Thanks DOM yeah i tried deleting the segue and recreating no luck... I'll keep workin at it – nwkeeley Jul 01 '13 at 18:15
  • Ok try adding a couple second delay in the connectionDidFinishLoading event in order to see if it is a race condition. Maybe your connection is finishing loading before the segue has been properly setup. – THE_DOM Jul 02 '13 at 16:32
  • @DOM: Yeah I got it working a different way... thank you for the help! – nwkeeley Jul 03 '13 at 17:06

1 Answers1

0

Just wanted to say I have this resolved altho I am not entirely sure how.

Basically this line in my ViewController

[authRequest authenticate];

Was loading a method in an external class file:

- (void)authenticate {
    NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:APIHOST path:@"/authenticate"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:3];
    NSString *postString = [[NSString alloc] initWithFormat:@"email=%@&password=%@", [username text], [password text]];

    [request setValue:APIKEY forHTTPHeaderField:@"apikey"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
    [request setURL:url];


    if ([NSURLConnection canHandleRequest:request]) {
        NSLog(@"Starting network connection");
        NSURLConnection *myConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [myConnection start];
    } else {
        //Error cannot activate network request from device
    }
}

The delegates for the NSURLConnection were in the viewcontroller... and I could see them being accessed when the method was complete however I couldn't perform the segue from them. By moving this function from the class file to my viewcontroller I was able to call my segue's from the delegate method of connectionDidFinishLoading....

Strange but worked.

nwkeeley
  • 1,397
  • 5
  • 18
  • 28