Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double tap outside action sheet popover presented using UIAlertController leads to going back to parent view in ios8 xcode 6 beta 5

I have a action sheet which i am presenting using UIAlertcontroller in ios8 (xcode 6 beta 5). I am using UIAlertcontroller because UIActionsheet ( which is deprecated in iOS 8 ) was not working properly in ios8, on click of any option in the actionsheet leaded me back to the parent view. Now I am facing one issue in UIAlertcontroller too, double tap outside the action sheet popover is leading me back to the previous parent view. Following is my code snippet:

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];
});
like image 805
user4045581 Avatar asked Sep 16 '14 09:09

user4045581


1 Answers

The following solution worked for me in my popover presentation controller scenario; I suspect the same bug underlies your situation:

  1. Set a delegate (conforms to UIPopoverPresentationControllerDelegate protocol) on your UIPopoverPresentationController.
  2. Implement the delegate method popoverPresentationControllerShouldDismissPopover:.

For example:

/**
 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;
}

Here's more context, where I'm setting up my popover presentation controller with a presented controller and a delegate:

// 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.
like image 54
idStar Avatar answered Oct 09 '22 02:10

idStar