2012-08-28 50 views
1

我正在使用警報視圖在互聯網未連接時提醒用戶。我在警報視圖中有兩個按鈕,它們都不起作用。我已經在.h文件中實現了。我用NSLog檢查點擊時是否響應。它在控制檯中響應,但按下按鈕時什麼也不做。這是我的代碼片段。UIAlertView按鈕不起作用

- (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],因爲我沒有導航控制器。任何想法,我在做什麼錯

回答

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"]]; 
    } 
} 

如果您還有任何問題,請讓我知道。

+0

我也試過。它不起作用。我的NSLog顯示點擊按鈕實際上給了我一個迴應,但我不能改變到另一個視圖。 – bumpfox