在通过 segue 更改视图之前检查并验证 JSON 数据字符串(iOS 应用程序 - 目标 C)
Posted
技术标签:
【中文标题】在通过 segue 更改视图之前检查并验证 JSON 数据字符串(iOS 应用程序 - 目标 C)【英文标题】:Check and validate JSON data string before changing view via segue (iOS App - Objective C) 【发布时间】:2014-06-04 23:02:31 【问题描述】:我正在开发一个通过 JSON 从 mysql 数据库获取数据的应用程序。我已经使用 NSJSONSerialization 创建了一个自定义表:
- (void)viewDidLoad
[super viewDidLoad];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:@"http://myurl.de"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
// Do any additional setup after loading the view, typically from a nib.
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
data = [[NSMutableData alloc]init];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
[data appendData:theData];
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
jsonContent = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Fehler" message:@"error" delegate:nil cancelButtonTitle:@"Schließen" otherButtonTitles:nil];
[errorView show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
现在我创建了一个登录视图,它应该在表格打开之前通过检查密码来验证用户帐户。我创建了一个文本字段和一个登录按钮,它有一个到表格视图的模式链接。
现在我的问题:php 文件提供了一个包含用户信息的 JSON 字符串,我不知道如何在 segue 打开另一个视图之前填充 jsonContent 数组。如何告诉程序运行连接函数来填充 jsonContent 数组?
我想证明 JSON 文件是否包含密码记录。如果不是这种情况,程序应该执行 NSAlert 并且 segue 应该停止。如果有记录,应该继续segue,我想将此记录提供给第二个视图。
我不明白为什么第一个自动视图会运行所有这些方法,而在我的新视图中却不是这样,当我触摸登录按钮时..
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
data = [[NSMutableData alloc]init];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
[data appendData:theData];
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
jsonContent = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Fehler" message:@"error." delegate:nil cancelButtonTitle:@"Schließen" otherButtonTitles:nil];
[errorView show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
if([segue.identifier isEqualToString:@"checkLogin"])
NSString *passcode = [_loginText text];
NSString *urlString = [[NSString alloc]initWithFormat:@"http://url.com/file.php&appcode=%@",passcode];
NSURL *passurl = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:passurl];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
NSLog(@"%@",jsonContent);
【问题讨论】:
【参考方案1】:您应该在按下登录按钮时发送请求,而不是在触发 segue 时发送请求。因为 NSURLRequests 在后台的另一个线程上执行,所以直到 segue 完成后的某个时间才会到达connectionDidFinishLoading
。因此,不要将 segue 设置为在 Interface Builder 中按下按钮时自动触发,而是将操作分配给可以启动请求的按钮。
- (IBAction)loginButtonPressed:(UIButton*)button
NSString *passcode = [_loginText text];
NSString *urlString = [[NSString alloc]initWithFormat:@"http://url.com/file.php&appcode=%@",passcode];
NSURL *passurl = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:passurl];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
jsonContent = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// check to see if jsonContent has what you want here and perform the segue if it does have it.
if (...)
[self performSegueWithIdentifier:@"checkLogin" sender:self];
【讨论】:
感谢您的帮助!我想过这样的事情,并且已经在隐藏 segue 触发器的同时创建了另一个 Button :) 如何访问 jsonContent NSArray 中的数据? :/ jsfiddle.net/reneil/Ap6FU 2014-06-05 00:17:37.511 [507:60b] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x16546d50 似乎有问题..请检查 jsfiddle :) 它会如果你也能帮助我,那就太好了 jsonContent 在这种情况下是NSDictionary
而不是 NSArray
。所以你会想要使用类似[jsonContent objectForKey:@"error"]
的东西。以上是关于在通过 segue 更改视图之前检查并验证 JSON 数据字符串(iOS 应用程序 - 目标 C)的主要内容,如果未能解决你的问题,请参考以下文章