0

I have two UIViewController, one is the main and from this trough a button you can go to the second. In SecondView.m I have the following code:

    - (IBAction)showpopup:(id)sender {
    [self becomeFirstResponder];
    UIMenuController *sharedController = [UIMenuController sharedMenuController];
    UIMenuItem *x2 = [[UIMenuItem alloc] initWithTitle:@"2x2" action: @selector(mat)];
    UIMenuItem *x3 = [[UIMenuItem alloc] initWithTitle:@"3x3" action: @selector(mat)];
    UIMenuItem *x4 = [[UIMenuItem alloc] initWithTitle:@"4x4" action: @selector(mat)];
    UIMenuItem *x5 = [[UIMenuItem alloc] initWithTitle:@"5x5" action: @selector(mat)];

    NSArray *menuArray = [NSArray arrayWithObjects: x2,x3,x4,x5, nil];


    CGRect drawRect = [sender convertRect:[sender bounds] toView: self.view];
    [sharedController setTargetRect:drawRect inView: self.view];

    [sharedController setMenuItems:menuArray];
    [sharedController setMenuVisible:YES animated:YES];
    [sharedController setMenuItems: nil];
}

-(BOOL)canBecomeFirstResponder{
    return YES;
}

-(int)mat:(id)sender{
    return 0;
}

The Button is linked as "touch up inside", but when I run the UIMenuController doesn't show up. The exact same code works in the main UIViewController.

Thanks

sergio
  • 68,819
  • 11
  • 102
  • 123
iAndrew
  • 71
  • 1
  • 9

1 Answers1

0

If I am not missing anything, I think you should, e.g., add your sharedController.view as a subview to your mainController.view, e.g. (assuming that `showpopup is defined in your main controller):

- (IBAction)showpopup:(id)sender {
    [self becomeFirstResponder];
    UIMenuController *sharedController = [UIMenuController sharedMenuController];
    ...
    [sharedController setMenuItems:menuArray];
    [sharedController setMenuVisible:YES animated:YES];
    [sharedController setMenuItems: nil];
    [self.view addSubview:sharedController.view];
}

or you could present modally your sharedController (replace the addSubview line above):

[self presentViewController:sharedController animated:YES completion:nil];

In any case, it seems to me that the "presenting" bit is missing.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • This is what I did before the edit and I thought I was wrong because I get this error http://d.pr/i/34wz – iAndrew Jan 08 '13 at 20:20
  • you are fully right, sorry. neither of my suggestions apply. could it be that the menu does not appear because you are resetting it with ` [sharedController setMenuItems: nil];`? try to remove that line... – sergio Jan 08 '13 at 21:08
  • how do you display your second controller from the first one? Is it in a navigation controller? tab bar controller? by doing `addSubview` with its view? – sergio Jan 09 '13 at 09:42
  • I display it though a UIButton that calls a "segue" and the second view appears – iAndrew Jan 10 '13 at 21:24