从connectionDidFinishLoading调用performSegueWithIdentifier的问题

时间:2013-06-27 13:51:37

标签: ios objective-c

我有一个登录屏幕,当按下按钮时,此代码执行:

- (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];


}

这很好用,segue按预期执行(所以我知道segue存在并且在故事板中正确识别)。然而,我不想在连接完成加载之前执行segue,所以这个实现文件是NSURLConnection的委托,在底部我(和响应代码上的if / else确实有效)..:

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

(当我在performDidFinishLoading中放入performSegueWithIdentifer时,我从上面注释掉performSegueWithIdentier ...)。

但现在应用程序总是崩溃说:

*由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:'Receiver(LoginViewController:0x7163640)没有带标识符'Login2Tab''的segue /强>

我被困在哪里因为它在connectionDidFinishLoading中没有被调用时有效...

1 个答案:

答案 0 :(得分: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的代表在viewcontroller中...当方法完成时我可以看到它们被访问但是我无法从它们执行segue。通过将此函数从类文件移动到我的viewcontroller,我可以从connectionDidFinishLoading的委托方法调用我的segue ....

奇怪但有效。