11
votes

I have an UIAlertView which is getting shown perfectly in iOS 7 but in iOS 8, it does not show any buttons or labels. Alert is still visible but just a small white box. The OK and cancel buttons take their events as well but no texts are visible.

I have used this alert to show on click of a button

- (IBAction)sel_btnLogout:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Logout!" message:@"Are you sure you want to logout?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    [alert show];
}

I checked the frame in iOS 8: it is giving (0,0,0,0) but in iOS 7 it is giving a definite value.

I also checked for iterating into the subviews of uialertview. In iOS7, it goes in the loop, as it finds alert's subviews. In iOS8, it says there are no subviews of alertView.

8
Is this the real code? Post the actual code if it is not.rmaddy
@siddhant post the real code. This code is working in iOS 8 also. So copy here the exact code you wrote in your project.Jasmeet
Jeev,This is the real code. I have just used this to show on click of a button.Siddhant
I checked the frame in iOS 8: it is giving (0,0,0,0) but in iOS 7 it is giving a definite value. I also checked for iterating into the subviews of uialertview. In iOS7, it goes in the loop, as it finds alert's subviews. In iOS8, it says there are no subviews of alertView.Siddhant

8 Answers

29
votes

Check if the class is available

if ([UIAlertController class])
{

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert title" message:@"Alert message" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:ok];

    [self presentViewController:alertController animated:YES completion:nil];

} 
else 
{

    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Alert title" message:@"Alert message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    [alert show];

}
12
votes

With iOS 8 you can set the title instead of the message:

[[[UIAlertView alloc] initWithTitle:@"AlertView in iOS 8." message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]show];

UIAlertView is deprecated from iOS 8 for more information visit this

http://nshipster.com/uialertcontroller/. https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/index.html

So if you're going to write separate code for iOS 7 and iOS 8, you should be using UIAlertController instead:

UIAlertController *alertController = [UIAlertController  alertControllerWithTitle:@"AlertView in iOS 8"  message:nil  preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
 [self dismissViewControllerAnimated:YES completion:nil];
}]];
[self presentViewController:alertController animated:YES completion:nil];
4
votes

I got the answer to my issue. The issue was that I was using UIFont+Replacement category in my project. This was working fine on iOS 7 but on iOS 8 it was using few deprecated methods. Due to this, I don't know why, but only my alert view was not showing any labels.

Solution: Deleted the category from the project and set font through xib. Once we place the .tff file of any font in our project workspace, we see those font names in the xib under custom fonts. NO NEED TO USE UIFont+Replacement category.

3
votes

Please read through the code below to understand how to add the fields and buttons to the alerts.

- (IBAction)UIAlertControllerWithActionSheetTextFields:(id)sender {
    UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:@"Info"
                                  message:@"You are using UIAlertController with Actionsheet and text fields"
                                  preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* ok = [UIAlertAction
                         actionWithTitle:@"OK"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             NSLog(@"Resolving UIAlert Action for tapping OK Button");
                             [alert dismissViewControllerAnimated:YES completion:nil];
                             
                         }];
    UIAlertAction* cancel = [UIAlertAction
                             actionWithTitle:@"Cancel"
                             style:UIAlertActionStyleDefault
                             handler:^(UIAlertAction * action)
                             {
                                 NSLog(@"Resolving UIAlertActionController for tapping cancel button");
                                 [alert dismissViewControllerAnimated:YES completion:nil];
                                 
                             }];
    
    [alert addAction:ok];
    [alert addAction:cancel];
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
       textField.accessibilityIdentifier = @"usernameTextField";
        textField.placeholder = @"Enter your username";
        textField.accessibilityLabel = @"usernameLabel";
    }];
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
       textField.placeholder = @"Enter your password";
       textField.accessibilityIdentifier = @"paswordTextField";
       textField.accessibilityLabel = @"passwordLabel";
    }];
    
    [self presentViewController:alert animated:YES completion:nil];
    
}

and if you need a project to completely refer the types of Alerts that are available in IOS, please follow my project from the below URL:

Alert variations in IOS

2
votes

in iOS 8 you need to replace UIAletrview and UIActionSheet with UIAlertcontroller . You read first This documentation in apple forum
Apple Alertcontroller

1
votes

You can check that code

if (([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] == NSOrderedAscending))
{
    // use UIAlertView
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:Title message:desc delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
}
else {
    // use UIAlertController
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:Title message:desc preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alert addAction:okAction];
    [self presentViewController:alert animated:YES completion:nil];
}
0
votes
let alert = UIAlertController(title: "Warning" as String , message: messageString as String, preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default)
        {
            UIAlertAction in
                self.dismissViewControllerAnimated(true, completion: nil)
        }
        // Add the actions
        alert.addAction(okAction)
        self.presentViewController(alert, animated: true, completion: nil)
-5
votes

i think this is not UIAlertview Issues. please check this work fine..

i think your code issues...........

in any view controller put this code in viewDidLoad Like below:

- (void)viewDidLoad 
{
    [super viewDidLoad];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"My message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    [alert show];
}