2014-11-03 12 views
3

爲什麼UIAlertController在iOS7接受零值,而需要提出的,但曾在iOS8上是好的,可我知道那是因爲iOS7不支持UIAlertController類?爲什麼iOS7中的UIAlertController收到零值?

UIAlertController *view=[UIAlertController 
         alertControllerWithTitle:@"Hello" 
         message:nil 
         preferredStyle:UIAlertControllerStyleAlert]; 

[self presentViewController:view animated:NO completion:nil]; 
+0

UIAlertController是爲iOS 8引入的。它不適用於iOS7。對於iOS7,您應該使用UIAlertView。 – Sreejith 2014-11-03 08:09:06

+0

http://stackoverflow.com/questions/25111011/uialertview-uialertcontroller-ios-7-and-ios-8-compatibility – Sreejith 2014-11-03 08:09:24

+0

我已經創建了一個可以既可以使用一個簡單的包裝類。它模仿UIAlertController。 https://github.com/Reggian/RAAlertController – Reggian 2015-01-30 14:49:13

回答

3

爲了你可以使用下面的代碼都iOS 8和更低的版本顯示AlertView

if ([self isiOS8OrAbove]) { 
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title 
                      message:message 
                     preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" 
                 style:UIAlertActionStyleDefault 
                handler:^(UIAlertAction *action) { 
                 [self.navigationController popViewControllerAnimated:YES]; 
                }]; 

    [alertController addAction:okAction]; 
    [self presentViewController:alertController animated:YES completion:nil]; 
} else { 
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:title 
                 message:message 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles: nil]; 
    [alertView show]; 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

- (BOOL)isiOS8OrAbove { 
    NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"8.0" 
                     options: NSNumericSearch]; 
    return (order == NSOrderedSame || order == NSOrderedDescending); 
} 
+0

但我認爲檢查類比檢查系統版本更好? – Deeper 2014-11-03 23:50:34

0

你可能在使用的操作系統版本小於的iOS 8

如果您在Xcode 6編譯代碼,並使用設備的iOS版本< 8.0,那麼你將面臨這個問題。我建議以下內容

- (void)showXYZAlert 
{ 
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) 
    { 
     // Handle UIAlertController 
     [self showXYZAlertController]; 
    } 
    else 
    { 
     // Handle UIAlertView 
     [self showXYZAlertControllerView]; 
    } 
} 
相關問題