Search code examples
iosobjective-cuialertcontrolleruimenucontroller

How present UIMenuController and UIAlertController at the same time?


I'm trying next way:

Message *message = self.messagesArray[indexPath.row];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:LocalizedString(@"FirstAction")
                                                                         message:@""
                                                                  preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *editMessage = [UIAlertAction actionWithTitle:LocalizedString(@"SecondAction") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:editMessage];
UIAlertAction *forwardMessage = [UIAlertAction actionWithTitle:LocalizedString(@"ThirdAction") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:forwardMessage];
UIAlertAction *deleteMessage = [UIAlertAction actionWithTitle:LocalizedString(@"DeleteMessage") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:deleteMessage];
[self setSourceViewForAlertController:alertController];
[self presentViewController:alertController animated:YES completion:nil];
[self createMenuForMessage:message];
[[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];

So, my aim is to present alertcontroller for UICollectionViewCell and UIMenuController at the same time.

Like this: enter image description here


Solution

  • Your issue is related to where are you showing your UIMenuController you have to take in account that UIView must have implemented the canBecomeFirstResponder method returning YES

    - (IBAction)action:(id)sender {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
                                                                                 message:@""
                                                                          preferredStyle:UIAlertControllerStyleActionSheet];
    
        UIAlertAction *editMessage = [UIAlertAction actionWithTitle:@"Edit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [alertController addAction:editMessage];
        UIAlertAction *forwardMessage = [UIAlertAction actionWithTitle:@"Move" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [alertController addAction:forwardMessage];
        UIAlertAction *deleteMessage = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [alertController addAction:deleteMessage];
        [self presentViewController:alertController animated:YES completion:nil];
        [[UIMenuController sharedMenuController] setTargetRect:self.view.bounds inView:self.view];
        [[UIMenuController sharedMenuController] setArrowDirection:UIMenuControllerArrowDefault];
        [[UIMenuController sharedMenuController] setMenuItems:@[[[UIMenuItem alloc]initWithTitle:@"test" action:@selector(didReceiveMemoryWarning)]]];
        [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
    }
    
    -(BOOL)canBecomeFirstResponder{
        return true;
    }
    

    enter image description here