【问题标题】:Disable Alert-view When connect to the Internet [closed]连接到 Internet 时禁用警报视图 [关闭]
【发布时间】:2012-11-22 21:38:06
【问题描述】:

我有一个警报视图,当没有互联网连接时会弹出。我有办法在有互联网连接时禁用它…… 工作代码:

-(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];

}

【问题讨论】:

    标签: iphone ios xcode ipad alert


    【解决方案1】:

    您可以将 alertView 保留为实例变量,然后在您的 iVar 上调用 didDismissWithButtonIndex。因此,您可以在 viewDidLoad 中分配警报并在之后使用它:

    -(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

    【讨论】:

    • 您的代码甚至没有显示警报视图...。
    • 我想在有互联网连接时禁用警报视图...。
    • 如果您注意到了,我在您的代码末尾添加了一个方法,可以让您关闭警报。在if 语句中(所以当有互联网连接时),调用该方法。您是否在头文件中实现UIAlertViewDelegate
    • 您的代码可以工作,但是当您连接到互联网时,警报不会消失...
    • 你有没有测试过自己的代码是否不适合我...... ..
    【解决方案2】:
    • (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

    用 buttonIndex 0 关闭它。

    【讨论】:

    • - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { } }
    【解决方案3】:

    您可以使用以下实例方法关闭UIAlertViews:

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

    [alert dismissWithClickedButtonIndex:0 animated:YES];
    

    当然,您首先需要找到对该警报视图的引用。

    UIAlertView class reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      相关资源
      最近更新 更多