iOS 8: UIAlertView / UIAlertController not showing text or buttons - ios

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.

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

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

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.

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

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

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

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)

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

Related

actionSheet:didDismissWithButtonIndex: is deprecated in iOS 8.3. What is the new method I have to use now?

These are deprecated, but I don't find the solution to improve it :
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(#"Take Photo", nil) style:UIAlertActionStyleDefault handler:^(__unused UIAlertAction *action)
{
[self actionSheet:nil didDismissWithButtonIndex:0];
}]];
And:
[[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(#"Take Photo", nil), NSLocalizedString(#"Photo Library", nil), nil] showInView:controller.view];
Finally:
- (void)actionSheet:(__unused UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
switch (buttonIndex)
{
case 0:
{
sourceType = UIImagePickerControllerSourceTypeCamera;
break;
}
case 1:
Many thanks.
You can use UIAlerController as UIActionSheet is deprecated after iOS 8.3.
Please have look at below code for your reference.
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:nil // Must be "nil", otherwise a blank title area will appear above our two buttons
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* button0 = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
// UIAlertController will automatically dismiss the view
}];
UIAlertAction* button1 = [UIAlertAction
actionWithTitle:#"Camera"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
// The user tapped on "Camera"
}];
UIAlertAction* button2 = [UIAlertAction
actionWithTitle:#"Photo Library"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
// The user tapped on "Camera"
}];
[alert addAction:button0];
[alert addAction:button1];
[alert addAction:button2];
[self presentViewController:alert animated:YES completion:nil];
Hope t his will guide you to get into UIAlterController in replace of UIActionSheet.
Thanks.
From Apple's website, It is clearly said you should use UIAlertController

can someone help me to fix this error

UIAlertController *alert = [[UIAlertController alloc] initWithTitle: alertString message:nil
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
I'm having this error:
No visible #interface for 'UIAlertController' declares the selector 'show'
and this : No visible #interface for 'UIAlertController' declares the selector 'initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:'
This true of step to declare UIAlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
// add action button
UIAlertAction *okAction = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:okAction]; // add action button to alert controller
// present alert controller in view
[self presentViewController:alertController animated:YES completion:nil];

iOS 7 -(void)setupAlertCtr:(NSString*)choseMenuType compatibility

-(void)setupAlertCtr:(NSString*)choseMenuType
{
self.alertContForNew = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"İptal" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)
{
}];
[self.alertContForNew addAction:cancel];
}
This code work on iOS 8 but when i try to use on iOS 7 get crash whats the problem ideas ?
Because UIAlertController is only support for iOS 8 and later, in iOS 7 we still have to use UIActionSheet or UIAlertView
so you have to put condition like below
if (ios8)// change your condition which check ios8 or later.
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Title" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* alertAction1 = [UIAlertAction actionWithTitle:#"Your title" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
//do what you want
}];
[alert addAction:cancelAction];
[alert addAction:alertAction1];
[self presentViewController:alert animated:YES completion:nil];
}
else
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:#"Title" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Your Title",nil];
[actionSheet showInView:self.view];
}
and for iOS 7 you have to implement <UIActionSheetDelegate> and its method
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
//do what you want.
break;
case 1:
break;
default:
break;
}
}

UIAlertView Vs UIAlertController - no keyboard in iOS 8

I have a old Xcode project that is targeted for iOS 6 and higher. I recently opened it up in Xcode 6 and ran in iPhone 6 simulator for iOS 8. When I tried this action
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter Folder Name"
message:#"Keep it short and sweet"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
dialog.tag = 400;
[dialog show];
I get a pop-up window but when I click on the text field, no keyboard comes up. I googled and read that I need to use UIAlertController instead. Since I need to support iOS 6, 7 versions as well so I changed my code like this.
if ([UIAlertController class])
{
// use UIAlertController
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:#"Enter Folder Name"
message:#"Keep it short and sweet"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action){
//Do Some action here
UITextField *textField = alert.textFields[0];
NSLog(#"text was %#", textField.text);
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
NSLog(#"cancel btn");
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"folder name";
textField.keyboardType = UIKeyboardTypeDefault;
}];
[self presentViewController:alert animated:YES completion:nil];
}
else
{
// use UIAlertView
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter Folder Name"
message:#"Keep it short and sweet"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
dialog.tag = 400;
[dialog show];
}
Again if I try the same action NO keyboard is displayed.
Is this a bug in Xcode 6 simulator or I am doing something wrong?
I Tested this, if iOS8 detects a hardware keyboard , than it does not open the keypad. As you might be testing in simulator , so it is not Showing any keypad
Press "Command" + "k" and you should be able to see the keypad.
When testing on a device you will not face this issue , unless user has connected his device with a hardware Bluetooth keypad , so this is not something you should worry about
Hope this helps
Another way of seeing the keyboard is setting a keyboard type. This could be an example code:
UIAlertController* alertController = [UIAlertController
alertControllerWithTitle:#"Test"
message:#"Testing keyboard"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler: ^(UITextField *tf)
{
tf.autocorrectionType = UITextAutocorrectionTypeYes;
tf.keyboardType = UIKeyboardTypeDefault;
}];
[self presentViewController:alertController animated:YES completion:nil];
Then you will see simulator's keyboard when you show the AlertController

Adding a simple UIAlertView

What is some starter code I could use to make a simple UIAlertView with one "OK" button on it?
When you want the alert to show, do this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ROFL"
message:#"Dee dee doo doo."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
// If you're not using ARC, you will need to release the alert view.
// [alert release];
If you want to do something when the button is clicked, implement this delegate method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
// the user clicked OK
if (buttonIndex == 0) {
// do something here...
}
}
And make sure your delegate conforms to UIAlertViewDelegate protocol:
#interface YourViewController : UIViewController <UIAlertViewDelegate>
Other answers already provide information for iOS 7 and older, however UIAlertView is deprecated in iOS 8.
In iOS 8+ you should use UIAlertController. It is a replacement for both UIAlertView and UIActionSheet. Documentation: UIAlertController Class Reference. And a nice article on NSHipster.
To create a simple Alert View you can do the following:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Title"
message:#"Message"
preferredStyle:UIAlertControllerStyleAlert];
//We add buttons to the alert controller by creating UIAlertActions:
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:#"Ok"
style:UIAlertActionStyleDefault
handler:nil]; //You can use a block here to handle a press on this button
[alertController addAction:actionOk];
[self presentViewController:alertController animated:YES completion:nil];
Swift 3 / 4 / 5:
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
//We add buttons to the alert controller by creating UIAlertActions:
let actionOk = UIAlertAction(title: "OK",
style: .default,
handler: nil) //You can use a block here to handle a press on this button
alertController.addAction(actionOk)
self.present(alertController, animated: true, completion: nil)
Note, that, since it was added in iOS 8, this code won't work on iOS 7 and older. So, sadly, for now we have to use version checks like so:
NSString *alertTitle = #"Title";
NSString *alertMessage = #"Message";
NSString *alertOkButtonText = #"Ok";
if (#available(iOS 8, *)) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:alertOkButtonText, nil];
[alertView show];
}
else {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
//We add buttons to the alert controller by creating UIAlertActions:
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:alertOkButtonText
style:UIAlertActionStyleDefault
handler:nil]; //You can use a block here to handle a press on this button
[alertController addAction:actionOk];
[self presentViewController:alertController animated:YES completion:nil];
}
Swift 3 / 4 / 5:
let alertTitle = "Title"
let alertMessage = "Message"
let alertOkButtonText = "Ok"
if #available(iOS 8, *) {
let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
//We add buttons to the alert controller by creating UIAlertActions:
let actionOk = UIAlertAction(title: alertOkButtonText,
style: .default,
handler: nil) //You can use a block here to handle a press on this button
alertController.addAction(actionOk)
self.present(alertController, animated: true, completion: nil)
}
else {
let alertView = UIAlertView(title: alertTitle, message: alertMessage, delegate: nil, cancelButtonTitle: nil, otherButtonTitles: alertOkButtonText)
alertView.show()
}
UPD: updated for Swift 5. Replaced outdated class presence check with availability check in Obj-C.
UIAlertView is deprecated on iOS 8. Therefore, to create an alert on iOS 8 and above, it is recommended to use UIAlertController:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Title" message:#"Alert Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:#"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
// Enter code here
}];
[alert addAction:defaultAction];
// Present action where needed
[self presentViewController:alert animated:YES completion:nil];
This is how I have implemented it.
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Title"
message:#"Message"
delegate:nil //or self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert autorelease];
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:#"Title"
message:#"Message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok",nil];
[myAlert show];
As a supplementary to the two previous answers (of user "sudo rm -rf" and "Evan Mulawski"), if you don't want to do anything when your alert view is clicked, you can just allocate, show and release it. You don't have to declare the delegate protocol.
Here is a complete method that only has one button, an 'ok', to close the UIAlert:
- (void) myAlert: (NSString*)errorMessage
{
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:errorMessage
message:#""
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"ok", nil];
myAlert.cancelButtonIndex = -1;
[myAlert setTag:1000];
[myAlert show];
}
This page shows how to add an UIAlertController if you are using Swift.
Simple alert with array data:
NSString *name = [[YourArray objectAtIndex:indexPath.row ]valueForKey:#"Name"];
NSString *msg = [[YourArray objectAtIndex:indexPath.row ]valueForKey:#"message"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:name
message:msg
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];

Resources