Why did UIAlertController get null in iOS7?

why did the UIAlertController in iOS7 get null while it was presented, but it worked in iOS8, it was good, can I know if this is because iOS7 doesn't support the UIAlertController class?

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

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

      

+3


source to share


3 answers


This class is only available in iOS8 and later. See Link



+4


source


For display AlertView

in iOS 8

and lower versions, you can use the following code:



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);
}

      

+3


source


You may be using os version less than iOS 8 .

If you compile your code with Xcode 6 and are using a device with iOS version <8.0, then you will face this problem. I would suggest the following

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

      

0


source







All Articles