5

我有一个操作表,我在 ios8 (xcode 6 beta 5) 中使用 UIAlertcontroller 呈现。我正在使用 UIAlertcontroller,因为 UIActionsheet(在 iOS 8 中已弃用)在 ios8 中无法正常工作,单击操作表中的任何选项都会让我回到父视图。现在我在 UIAlertcontroller 中也面临一个问题,在操作表弹出框外双击会导致我回到之前的父视图。以下是我的代码片段:

UIAlertController *actionSheetIos8;

actionSheetIos8 = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

NSArray *buttonsArray = [self returnMoreArray];
int startY = 10;
for (int i = 0; i < [buttonsArray count]; i++) {

    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:[buttonsArray objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSString *newStr = [buttonsArray objectAtIndex:i];
        newStr = [[newStr lowercaseString] stringByReplacingOccurrencesOfString:@" " withString:@"_"];
    }];
    [actionSheetIos8 addAction:defaultAction];        
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];

[actionSheetIos8 setModalPresentationStyle:UIModalPresentationPopover];
UIPopoverPresentationController *popPresenter = [actionSheetIos8
                                                 popoverPresentationController];
popPresenter.sourceView = sender;
popPresenter.sourceRect = [sender frame];
dispatch_async(dispatch_get_main_queue(), ^ {
    [self presentViewController:actionSheetIos8 animated:YES completion:nil];
});
4

1 回答 1

13

以下解决方案在我的 popover 演示控制器场景中为我工作;我怀疑同样的错误是你的情况的基础:

  1. UIPopoverPresentationControllerDelegate在您的UIPopoverPresentationController.
  2. 实现委托方法popoverPresentationControllerShouldDismissPopover:

例如:

/**
 The presence of this delegate callback inhibits the popover presentation controller
 from mistakenly calling 'dismissViewControllerAnimated:completion:' on us twice.
 */
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController;
{
    NSLog(@"delegate method called asking permission to dismiss popover");
    return YES;
}

这里有更多的上下文,我正在使用呈现的控制器和委托设置我的 popover 呈现控制器:

// Set modal presentation style and issue the presentViewController:animated:completion:
// call before retrieving popover presentation controller created for us, as suggested
// in the API documentation that is more recent than the sample code from the
// WWDC2014 slides in Session 228:
presentedController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:presentedController animated:YES completion:nil];

// Now we can retrieve the popover presentation controller and configure it:
UIPopoverPresentationController *popPC = presentedController.popoverPresentationController;
popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
CGRect popoverRect = cell.infoButton.bounds; // Assume 'cell' exists; it's an object of mine with an 'infoButton' view.
popPC.sourceView = cell.infoButton; // has to be provided along with sourceRect
popPC.sourceRect = popoverRect;  // has to be provided along with sourceView
popPC.delegate = self; // or whomever you set to be your popover presentation controller delegate.
于 2014-09-28T14:01:55.783 回答