2012-11-22 62 views
-2

我有一個警報視圖,當沒有互聯網連接時彈出。當有互聯網連接時,我有辦法禁用它...。 工作代碼:禁用警報視圖連接到互聯網時

-(void)reachabilityChanged:(NSNotification*)note 

    { 
     Reachability * reach = [note object]; 

     if([reach isReachable]) 
     { 
      notificationLabel.text = @"Notification Says Reachable"; 
      NSLog(@"Internet is Up"); 



     } 
     else 
     { 
      notificationLabel.text = @"Notification Says Unreachable"; 
      NSLog(@"Internet is Down"); 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please connect to Internet" 
                  message:nil 
                  delegate:self 
                cancelButtonTitle:nil 
                otherButtonTitle:nil 
      [alert show]; 
     } 
    } 

-(void)dismissAlert:(UIAlertView *)alertView{ 
      [alertView dismissWithClickedButtonIndex:0 animated:YES]; 

}

回答

1

你可以保持alertView作爲實例變量,然後調用didDismissWithButtonIndex你的伊娃。所以,你可以在你的viewDidLoad中的Alloc警報後使用它:

-(void)reachabilityChanged:(NSNotification*)note{ 
     Reachability * reach = [note object]; 
     if([reach isReachable]) 
     { 
      notificationLabel.text = @"Notification Says Reachable"; 
      NSLog(@"Internet is Up"); 
      [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:0]; 
     } 
     else 
     { 
      notificationLabel.text = @"Notification Says Unreachable"; 
      NSLog(@"Internet is Down"); 
      //or you can realloc here your alert 
      UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)]; 
      progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; 
      [alert addSubview:progress]; 
      [progress startAnimating]; 
      [alert show]; 
     } 
    } 

    -(void)dismissAlert:(UIAlertView *)alertView{ 
       [alertView dismissWithClickedButtonIndex:0 animated:YES]; 
    } 

並確保在你的頭文件來實現UIAlertViewDelegate

+0

您的簡化版,代碼甚至顯示警報視圖...。 –

+0

我想在有互聯網連接時禁用警報視圖...。 –

+0

如果您已經注意到,我在代碼的末尾添加了一種方法,可以讓您關閉警報。在if語句中(所以當有互聯網連接時),就會調用該方法。你在頭文件中實現了'UIAlertViewDelegate'嗎? – Mat

0
  • (無效)alertView:(UIAlertView中*)alertView didDismissWithButtonIndex:(NSInteger的)buttonIndex

與駁回buttonIndex 0.

+0

- (空)alertView:(UIAlertView中*)alertView didDismissWithButtonIndex:(NSInteger的)buttonIndex { 如果(buttonIndex == 0){ } } –

+0

沒有什麼事情發生... –

0

您可以關閉UIAlertView s的下列實例方法:

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated 

[alert dismissWithClickedButtonIndex:0 animated:YES]; 

當然,你需要先找到該警報的視圖的引用。

UIAlertView class reference