我的應用程序提取了一個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