iOS 8: UIAlertView / UIAlertController not showing text or buttons

54,452

Solution 1

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.

Solution 2

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

}

Solution 3

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

Solution 4

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

Solution 5

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

Share:
54,452
Siddhant
Author by

Siddhant

Updated on July 09, 2022

Comments

  • Siddhant
    Siddhant almost 2 years

    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.

  • rmaddy
    rmaddy over 9 years
    1) Do not show an alert from viewDidLoad. The view isn't showing yet. 2) How does this answer the question? The OP states the code works under iOS 7 so it is not a problem with where the alert is being shown.
  • rmaddy
    rmaddy over 9 years
    This makes no attempt to answer the question. The OP wants both a title and message which is still perfectly fine with UIAlertView under iOS 8.
  • Siddhant
    Siddhant over 9 years
    @rmaddy - Alert is still shown in iOS8 but no texts or button labels are shown. I checked the frame...it gives (0,0,0,0)
  • rmaddy
    rmaddy over 9 years
    @Siddhant Put that info in your question, not this unrelated answer.
  • Siddhant
    Siddhant over 9 years
    I have put that info. Sorry forgot earlier.
  • Siddhant
    Siddhant over 9 years
    @rmaddy - Please see my updated info. I checked for heirarchy of UIAlertView in iOS7 and iOS 8. in iOS7, it shows child views but in iOS8, it does not show any child views.
  • rmaddy
    rmaddy over 9 years
    @Siddhant Your comments do not apply to this answer. Add your comments below the question.
  • Siddhant
    Siddhant over 9 years
    @rmaddy - Done. Hope the issue is clear now? Please help
  • Alex Guerra
    Alex Guerra over 9 years
    It works :D. Thank you very much, Siddhant. I was going crazy!!
  • Siddhant
    Siddhant over 9 years
    @AlexGuerra - You are welcome. Kind of strange issue and even a more strange solution :)