Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel button is not showing for a UIAlertController in iPad, iOS8 and in objective-C

I am trying to use UIAlertController. I need a popover with two buttons -- "Cancel" and "Delete and Sign Out". But I can only see the "Delete and Sign out" button and not the cancel button. Here 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;
popPresenter.sourceRect = logoutButton.bounds;
[self presentViewController:alert animated:YES completion:nil];

Unfortunately, I am unable to post images, so please let me know if you need anymore clarifications.

like image 241
Karish Avatar asked Mar 24 '15 20:03

Karish


1 Answers

On iOS8 the cancel button is shown only when needed. If you run the app on iPhone it is visible. If you run the app on iPad the cancel button is not shown and the handler for the cancel action (style:UIAlertActionStyleCancel) is called when the user taps outside the popup.

like image 163
a_malika Avatar answered Nov 04 '22 09:11

a_malika