我已经设置了我的应用程序,以便每个应用程序都进入前台,它会进行Web调用,从Web服务器调用信息,接收并解析JSON,然后将其存储在脱机状态。
我的应用程序是一个标签式应用程序,每个屏幕的信息都是从JSON设置的,图像也是从服务器中提取的。所以我希望每个页面上的信息在完成后更新,但不会更新。
我正在从模拟器运行我的应用程序,暂时不会产生任何影响。目前只有我可以获取要更新的信息的方法是使用viewDidAppear或者从xCode再次停止并启动应用程序。
我尝试从AppDelegates
调用类viewDidLoad - (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
*/
WebCalls *wc = [[WebCalls alloc] init];
DashboardVC *db = [[DashboardVC alloc] init];
[wc setWebCallDidFinish:^(NSString * json, NSString *ID) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString *docFile = [docDir stringByAppendingPathComponent: @"json.txt"];
[json writeToFile:docFile atomically:NO encoding:NSUTF8StringEncoding error:Nil];
NSString *docDir2 = [paths objectAtIndex: 0];
NSString *docFile2 = [docDir2 stringByAppendingPathComponent: @"theID.txt"];
[ID writeToFile:docFile2 atomically:NO encoding:NSUTF8StringEncoding error:Nil];
[db testme];
}];
[wc getData];
}
这会再次调用viewDidLoad,但信息不会更新。有什么想法吗?
编辑:在该课程中,它会像这样读取JSON
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString *docFile = [docDir stringByAppendingPathComponent: @"json.txt"];
[json writeToFile:docFile atomically:NO encoding:NSUTF8StringEncoding error:Nil];
我可以打印JSON,JSON肯定会更新,但屏幕上的信息不是。
编辑:已更新为网络电话
答案 0 :(得分:0)
您可以尝试使用委托方法将获取的数据更新到UI中。在JSON解析结束后调用委托方法。我正在使用xml解析做同样的事情并且它正在工作。
(void)parserDidEndDocument:(NSXMLParser *)解析器{
[self.delegate optimizeAndDrawShortestPath:coordinatesArray];
[routeArray release]; routeArray = nil; }
检查上面的例子。这是我解析xml文档的方法。 然后,正如您在第一行中看到的,我调用委托方法来更新UI并在地图上绘制线条。委托方法在调用我的解析类的对象的类中实现。
我认为,你正在做与JSON类似的事情,就像我在使用XML一样。了解协议和委托方法check this SO answer.
我很确定,这就是你要找的东西。
答案 1 :(得分:0)
解决了这个问题。在应用程序中设置定期Web调用,并在进行Web调用时,在类中调用refresh方法。