Deeper
Deeper

Reputation: 129

why UIAlertController in iOS7 received nil value?

why UIAlertController in iOS7 received nil value while need to presented but worked in iOS8 was good, may i know that is because iOS7 not support the UIAlertController class ?

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

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

Upvotes: 3

Views: 1130

Answers (3)

BLC
BLC

Reputation: 2300

You might be using os version less then iOS 8.

If you compile your code with Xcode 6 and use a device with iOS version < 8.0 then you will face this issue. 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];
    }
}

Upvotes: 0

Sujith Thankachan
Sujith Thankachan

Reputation: 3506

In order to display AlertView in both 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);
}

Upvotes: 3

Jens
Jens

Reputation: 69505

These class is only available in iOS8 and later. See the class reference

Upvotes: 4

Related Questions