我发现了许多类似的问题,但无论如何都找不到我的问题的解决方案。所以,我的应用程序(它由 tabView 组成)需要在第一次启动后从服务器下载数据。为此,它调用模态视图控制器,在其中使用 Reachability.h/m 和 NSNotification defaultCenter 检查互联网连接。如果可以访问互联网,则开始下载并保存数据。完成后,我想关闭模式打开的视图控制器,但它一遍又一遍地重新加载。我坚持了将近2天,请帮忙:)
这是我的代码的一部分:在第一个 ViewController
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//checking for data if not calls modalViewController
if(firstStart){
[self performSegueWithIdentifier:@"startDownload" sender:self];
}
}
在第二个(模态视图控制器)
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"];
[hostReachable startNotifier];
}
在 checkNetworkStatus 完成 检查互联网连接是否存在互联网连接 执行 JSON 下载和解析,最后调用 Close 方法
-(void)Close
{
[self dismissViewControllerAnimated:YES completion:nil];
}
在这个调用之后,屏幕转到第一个 ViewController 并立即变为第二个,所有的东西都再次执行,它有点卡在循环中。