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...

4

1 回答 1

0

只是想说我已经解决了这个问题,尽管我不完全确定如何解决。

基本上我的 ViewController 中的这一行

[authRequest authenticate];

在外部类文件中加载方法:

- (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
    }
}

NSURLConnection 的代表在视图控制器中......当方法完成时我可以看到它们正在被访问,但是我无法从它们执行 segue。通过将此函数从类文件移动到我的视图控制器,我能够从 connectionDidFinishLoading... 的委托方法调用我的 segue。

奇怪但有效。

于 2013-07-03T17:05:44.643 回答