ios - AFNetworking 成功崩溃应用后调用 segue

标签 ios afnetworking segue

我正在尝试为我的 ios 应用程序创建一个登录屏幕,我已经设法在身份验证后取回 json,但是当我尝试调用 segue 以在身份验证后从登录屏幕移动到仪表板时,它不起作用。

我假装做的是基本登录,用户将他的 API key 放在输入上,单击提交,如果没问题,它会转到仪表板 View Controller 。

检查我的代码:

- (IBAction)touchLogin:(id)sender {    

    NSURL *url = [NSURL URLWithString:@"https://api.site.com/"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request , NSHTTPURLResponse *response , id JSON)
    {
        NSLog(@"Got JSON: %@", JSON);
        [self performSegueWithIdentifier:@"LoginSuccesful" sender:self];
    }
    failure:^(NSURLRequest *request , NSHTTPURLResponse *response , NSError *error , id JSON){
        NSLog(@"Something went wrong. %@ - %@", [error localizedDescription], JSON);
    }];

    [operation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge)
    {
        if (challenge.previousFailureCount == 0)
        {

            NSURLCredential *creds = [NSURLCredential credentialWithUser:self.apiKeyTextField.text
                                                                password:@""
                                                             persistence:NSURLCredentialPersistenceForSession];

            [[challenge sender] useCredential:creds forAuthenticationChallenge:challenge];
        }
        else
        {
            NSString *errorString = @"Incorrect API Key";
            UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [errorAlertView show];

            NSLog(@"Previous auth challenge failed. Are username and password correct?");
        }
    }];

    [operation start];

}

我已经创建了 segue 并放置了标识符“LoginSuccesful”,我不确定是否应该在该代码块中调用 segue。

当我运行应用程序时,我得到带有 JSON 的 NSLog,但是当尝试执行 segue 并向我显示错误时应用程序崩溃
Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'LoginSuccesful'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.

但是如果你看一下我的 Storyboard文件,我已经创建了这个 segue,甚至放置了导航 Controller 。

enter image description here

有人有这个问题吗?有人可以让我知道身份验证正常后如何调用另一个 ViewController 吗?

即使对于错误警报 View ,我也不确定这是否是最好的方法,但它工作正常

最佳答案

选择第一个 View Controller (如屏幕截图所示)。然后,在菜单上,转到编辑器 -> 嵌入 -> 导航 Controller 。完成此操作后,不需要第二个导航 Controller 。您可以在 View Controller 和 Table View Controller 之间拖动 segue。正如您从错误中看到的那样,问题是“源 Controller ”没有“UINavigationController 的实例”。

关于ios - AFNetworking 成功崩溃应用后调用 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17038269/

相关文章:

iphone - NSString——静态的还是内联的?有任何性能提升吗?

iphone - 如何将 iOS 应用程序发送给客户,让他们进行代码签名

ios - 广场 API + OAuth : How do I sign out of my iPad app?

ios - 用于 JSON 解析的 AFNetworking 出错

objective-c - AFNetworking 显示上传进度

ios - JSON文本未以AFNetworking的数组错误开头

swift - 如何检测 View Controller 是否被解雇

swift - 检测 UIViewController 何时被 tvOS 中的 Remote 菜单按钮关闭

ios - UIlabel 问题 : Keep the string value after app is closed?

ios - 从 UITableViewCell 传递数据