我正在尝试为我的 ios 应用程序创建一个登录屏幕,我已经设法在身份验证后取回 json,但是当我尝试调用 segue 以在身份验证后从登录屏幕移动到仪表板时,它不起作用。
我假装做的是基本登录,用户将他的 API 密钥放在输入端,点击提交,如果没问题,它会转到仪表板视图控制器。
检查我的代码:
- (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.
但是如果你看一下我的故事板文件,我已经创建了这个 segue,甚至放置了导航控制器。
有人有这个问题吗?有人可以让我知道身份验证正常后如何调用另一个 ViewController 吗?
即使对于错误警报视图,我也不确定这是否是最好的方法,但它工作正常