我的应用程序提取了一个json数据馈送,我刚刚将可访问性检查添加到了应用程序委托中的applicationDidBecomeActive中。如果网络可联系,则视图控制器被加载。在视图控制器中,调用解析器类来填充UITableview。 如果网络不可联系,则会显示警报。如果用户按下主页按钮激活无线或蜂窝数据,然后返回到应用程序,则视图控制器将加载。虽然这可行,但解决方案并不特别优雅,因为每次用户在放入后台后都会返回到应用程序,ViewController调用将再次运行。我想知道有没有更好的选择?可访问性最佳实践
AppDelegate.m
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Check for network connectivity
reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Connection Error" message:@"App cannot connect to the internet" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
self.internetActive = NO;
}
else {
NSLog(@"internet available");
self.internetActive = YES;
}
// if internet available call viewcontroller;
if (self.internetActive) {
navController.viewControllers = [NSArray arrayWithObject:viewController];
[window addSubview:navController.view];
[window makeKeyAndVisible];
}
else {
//error pic background display
}
然后是viewcontroller.m
- (void)viewDidLoad {
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
[loadingActivityIndicator startAnimating];
Infoparser *cparser = [[FCO_parser alloc] init];
[cparser queryclistwithViewCont:self];
[cparser release];
[super viewDidLoad];
}
您确定viewDidLoad方法实际上是否被调用?NSLog(@“VC called”);我不确定你想要优化什么,因为无论如何你的视图必须在应用程序被唤醒时加载。 – Rayfleck 2011-03-23 18:17:26