在函數中有一個UIAlert:
- (void)loadJSON
{
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if (networkStatus == NotReachable) {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement" message: @"No Connection" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release];
});
} else {
//some code
}
}
在用戶點了OK鍵後應該觸發函數,回調loadJSON 。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[self loadJSON];
}
我的最終目標是如果沒有網絡連接,顯示UIAlert,用戶看見信息,點擊Okay,如果還沒有網絡連接,顯示警報。我沒有連上網,警報信息只顯示了一次。
不要給alertView分配代理:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement"
message: @"No Connection"
delegate: self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];