UIAlertController は外側をクリックすると閉じるハンドルです (iPad) 質問する

UIAlertController は外側をクリックすると閉じるハンドルです (iPad) 質問する

iOS8 より前では、アラートを表示するために UIActionSheet を使用していましたが、現在は UIAlertController を使用する必要があります。

UIActionSheet を使用すると、clickedButtonAtIndex と cancelButtonIndex を比較することで、ユーザーがポップアップの外側をクリックした (つまり、操作をキャンセルしたい) 状況を簡単に処理できます。ユーザーが実際にポップアップの外側を押した場合は、この関数でキャンセル ボタンのインデックスを取得します。

新しい UIAlertController でこれらの状況をどのように処理できますか? 「完了」ブロックを使用しようとしましたが、コンテキストがありません。これを処理する簡単な方法はありますか? (アクションの状態を一般的な変数に「保存」する以外に方法はありますか)。

ベストアンサー1

style:UIAlertActionStyleCancel を使用してアクションを追加することができ、ユーザーがポップアップの外側をタップすると、このアクションのハンドラーが呼び出されます。

if ([UIAlertController class]) {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"A Message" preferredStyle:UIAlertControllerStyleActionSheet];

    [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"User clicked button called %@ or tapped elsewhere",action.title);
    }]];

    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"User clicked button called %@",action.title);
    }]];

    [alertController addAction:[UIAlertAction actionWithTitle:@"Other" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"User clicked button called %@",action.title);
    }]];

    UIControl *aControl = (UIControl *) sender;
    CGRect frameInView = [aControl convertRect:aControl.bounds toView:self.view];
    alertController.popoverPresentationController.sourceRect = frameInView;
    alertController.popoverPresentationController.sourceView = self.view;
    alertController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
    [self presentViewController:alertController animated:YES completion:nil];
}

おすすめ記事