【问题标题】:Problems calling performSegueWithIdentifier from connectionDidFinishLoading从 connectionDidFinishLoading 调用 performSegueWithIdentifier 时出现问题
【发布时间】:2013-06-27 13:51:36
【问题描述】:

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

- (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 委托,并且在底部我有(并且 responseCode 上的 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."); 
 }

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

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

* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“接收方(LoginViewController:0x7163640)没有与标识符“Login2Tab”的segue”

这是我卡住的地方,因为当它没有从 connectionDidFinishLoading 中调用时它可以工作......

【问题讨论】:

  • 这两个方法(btnLogin:connectionDidFinishLoading:)是在同一个类(LoginViewController)中实现的吗?
  • 你试过this post的建议了吗?
  • @Guillaume 是的,它们都在 LoginViewController.m 中实现
  • @Corey,刚刚尝试了该帖子中的所有内容......没有运气。让我发疯,因为它在没有从该 connectionDidFinishLoading 调用时工作......所以我知道它的设置是正确的,只是不确定为什么它不能从委托方法中看到 segue......
  • 您确定 iPhone 和 iPad 故事板上都存在 segue 吗?可能您正在使用另一种不存在 segue 的方法?

标签: ios objective-c


【解决方案1】:

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

在我的 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 ....

奇怪但有效。

【讨论】:

    猜你喜欢
    • 2012-02-18
    • 2013-12-11
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    相关资源
    最近更新 更多