In iPad, iOS8, and Objective-C, UIALERTCONTROLLER does not display a cancel button

I am trying to use UIAlertController. I need a popup box with two buttons-“Cancel” and “Delete and Exit”. But I can only see “Delete and Exit” Button instead of cancel button. This is the code: –

NSString *confirmText = "Hi"; 
UIAlertController *alert = [UIAlertController alertControllerWithTitle:confirmText message :@"" preferredStyle:UIAlertControllerStyleActionSheet];


// Created a deleted action
UIAlertAction *destroyAction = [UIAlertAction actionWithTitle:@"Delete and Sign Out"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
NSLog(@"Delete Action Pressed");
[self signout];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:nil];


[alert.view setTintColor:[UIColor grayColor]];
[alert addAction:cancelAction];
[alert addAction:destroyAction] ;
[alert setModalPresentationStyle:UIModalPresentationPopover];

UIPopoverPresentationController *popPresenter = [alert popoverPresentationController];
// Set the sourceView.
popPresenter.sourceView = logoutButton;< br />popPresenter.sourceRect = logoutButton.bounds;
[self presentViewController:alert animated:YES completion:nil];

Unfortunately, I can’t post pictures, so if you need more Please let me know.

On iOS8, the cancel button is displayed only when needed. If you Run the application on the iPhone, it will be visible. If you run the application on the iPad, the cancel button will not be displayed, and when the user clicks outside the pop-up window, the cancel operation handler (style: UIAlertActionStyleCancel) will be called.

I am trying to use UIAlertController. I need a pop-up box with two buttons-“Cancel” and “Delete and Exit”. But I can only see the “Delete and Exit” button and Not the cancel button. This is the code: –

NSString *confirmText = "Hi"; 
UIAlertController *alert = [UIAlertController alertController WithTitle:confirmText message:@"" preferredStyle:UIAlertControllerStyleActionSheet];


// Created a deleted action
UIAlertAction *destroyAction = [UIAlertAction actionWithTitle:@"Delete and Sign Out"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
NSLog(@"Delete Action Pressed");
[self signout];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:nil];


[alert.view setTintColor: [UIColor grayColor]];
[alert addAction:cancelAction];
[alert addAction:destroyAction];
[alert setModalPresentationStyle:UIModalPre sentationPopover];

UIPopoverPresentationController *popPresenter = [alert popoverPresentationController];
// Set the sourceView.
popPresenter.sourceView = logoutButton;
popPresenter.sourceRect = logoutButton. bounds;
[self presentViewController:alert animated:YES completion:nil];

Unfortunately, I cannot post the picture, so if you need more clarification, please let me know.

On iOS8, the cancel button is displayed only when needed. If you run the app on an iPhone, it will be visible. If you run the app on an iPad, then The cancel button is not displayed, and when the user clicks outside the pop-up window, the cancel operation handler (style: UIAlertActionStyleCancel) will be called.

Leave a Comment

Your email address will not be published.