UIAlertControllerStyleActionSheet不起作用

时间:2014-12-15 04:33:09

标签: ios8 uialertcontroller

出于某种原因,这似乎不适用于我的iPad。我已经浏览了一些建议,说这是成功的,但我甚至无法让它发挥作用。有人可以向我解释一下吗?

- (IBAction)pressButton:(id)sender {

UIAlertController *alertController;
UIAlertAction *destroyAction;
UIAlertAction *otherAction;

alertController = [UIAlertController
                                      alertControllerWithTitle:@"Reason"
                                      message:@"Select the following"
                                      preferredStyle:UIAlertControllerStyleActionSheet];

destroyAction = [UIAlertAction actionWithTitle:@"Remove All Data"
                                         style:UIAlertActionStyleDefault
                                       handler:^(UIAlertAction *action) {
                                           // do destructive stuff here
                                       }];
otherAction = [UIAlertAction actionWithTitle:@"Blah"
                                       style:UIAlertActionStyleDefault
                                     handler:^(UIAlertAction *action) {
                                         // do something here
                                     }];

[alertController addAction:destroyAction];
[alertController addAction:otherAction];

[self presentViewController:alertController animated:YES completion:nil];

}

我自己进行测试,我尝试将preferredStyle:UIAlertControllerStyleActionSheet更改为preferredStyle:UIAlertControllerStyleAlert,但它确实有效。我错过了什么吗?不是ActionSheet和Alert之间的唯一区别是他们的首选风格吗?

1 个答案:

答案 0 :(得分:1)

当我尝试你的代码时,我遇到了异常并崩溃。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

实际上,[alertController addAction:nil]会导致异常。在iOS 8中使用UIAlertController时,您不再需要设置NIL按钮。 所以,删除它。

修改

创建一个IBoutlet,以便popPresenter知道弹出的位置:

@property (strong, nonatomic) IBOutlet UIButton *button;

在动作方法中添加以下代码:

[alertController setModalPresentationStyle:UIModalPresentationPopover];

//Create a popoverPresenter
UIPopoverPresentationController *popPresenter = [alertController
                                                 popoverPresentationController];
popPresenter.sourceView = self.button;
popPresenter.sourceRect = self.button.bounds;