1

我正在使用警报视图在未连接互联网时提醒用户。我在警报视图中有两个按钮,它们似乎都不起作用。我已经在 .h 文件中实现了。我使用 NSLog 检查单击时它是否响应。它在控制台中响应,但在按下按钮时什么也不做。这是我的代码片段。

- (IBAction)startButton:(id)sender 
   {
     if (![self connectedToNetwork])
     {
       UIAlertView *internetAlert = [[UIAlertView alloc] initWithTitle: @"Network Error!" message: @"You are not connected to the internet" delegate: self cancelButtonTitle: @"OK" otherButtonTitles: @"Open Settings", nil];
       [internetAlert show];
     }
   }

- (void)alertView: (UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
  { 
     if (buttonIndex == 0)
     {
        NSLog(@"Ok clicked");
        HomeViewController *blah = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
       [self.view addSubview: blah.view];
     }

     else if (buttonIndex == 1)
     {
        NSLog(@"Settings clicked");
        [[UIApplication sharedApplication] openURL: [NSURL URLWithString:@"prefs:root=WIFI"]];
     }
}

我使用 [self.contentView addSubview: blah.view] 因为我没有导航控制器。关于我做错了什么的任何想法

4

1 回答 1

0

在这里,您使用了错误的方法来单击 UIAlertView 按钮。您必须使用 UIAlertView 的另一种方法。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

请更改为以下代码,它将起作用。

- (IBAction)startButton:(id)sender 
{
    if (![self connectedToNetwork])
    {
        UIAlertView *internetAlert = [[UIAlertView alloc] initWithTitle: @"Network Error!" message: @"You are not connected to the internet" delegate: self cancelButtonTitle: @"OK" otherButtonTitles: @"Open Settings", nil];
        [internetAlert show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ 
    if (buttonIndex == 0)
    {
        NSLog(@"Ok clicked");
        HomeViewController *blah = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
        [self.view addSubview: blah.view];
    }

    else if (buttonIndex == 1)
    {
        NSLog(@"Settings clicked");
        [[UIApplication sharedApplication] openURL: [NSURL URLWithString:@"prefs:root=WIFI"]];
    }
}

如果您还有任何问题,请告诉我。

于 2012-08-28T05:31:46.670 回答