使用UIAlertController双击外部动作表弹出导致返回到ios8中的父视图xcode 6 beta 5

时间:2014-09-16 09:22:25

标签: objective-c ios8 xcode6-beta5

我有一个动作表,我在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];
});

1 个答案:

答案 0 :(得分:13)

以下解决方案适用于我的popover演示控制器场景;我怀疑同样的错误是你的情况的基础:

  1. UIPopoverPresentationControllerDelegate上设置代理(符合UIPopoverPresentationController协议)。
  2. 实施委托方法popoverPresentationControllerShouldDismissPopover:
  3. 例如:

    /**
     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.