iPad上的UIAlertController没有显示“取消”按钮,在iOS8和Objective-C中。

15

我正在尝试使用UIAlertController。我需要一个带有两个按钮的弹出窗口 -- "取消"和"删除并退出登录"。但是我只能看到"删除并退出登录"按钮而看不到取消按钮。以下是代码: -

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

非常抱歉,我无法发布图片,如果您需要更多澄清,请告诉我。


我测试了你的代码,一切都正常... 我在iOS8的iPad和iPhone上都看到了两个按钮。请包含你用来实例化警告控制器的代码。 - Chase
2
如果你想要展示一个警告框,只需将取消操作的style:更改为UIAlertActionStyleDefault - Albert Renshaw
或者在 UIAlertController 上使用 preferredStyle: .alert - Chris Prince
1个回答

28

iOS8中只在必要时显示取消按钮。如果您在iPhone上运行应用程序,则会显示该按钮。如果您在iPad上运行应用程序,则不会显示取消按钮,并且当用户点击弹出窗口外部时,将调用取消操作的处理程序(style:UIAlertActionStyleCancel)。


7
另外,如果您想要显示一个弹窗,只需将取消操作的 style: 更改为 UIAlertActionStyleDefault - Albert Renshaw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接