Search code examples
iphoneiosios4ios5uimenucontroller

UIMenuController does not show on iOS 4, but works on iOS 5


In a custom view i have set 'canBecomeFirstResponder' to return YES and have also defined '- (BOOL)canPerformAction:(SEL)action withSender:(id)sender'

I use the following code to display the popup menu:

[self becomeFirstResponder];
UIMenuController *theMenu = [UIMenuController sharedMenuController];


if (theMenu.menuItems==nil)
{
    UIMenuItem* item = [[UIMenuItem alloc] initWithTitle:@"Open" action:@selector(openAction:)];
    NSArray* array = [NSArray arrayWithObject:item];
    theMenu.menuItems = array;
    [item release];
}

CGRect rect;
rect = CGRectMake(self.frame.size.width*0.5, self.frame.size.height*0.5, 0, 0);
[theMenu setTargetRect:rect inView:self];
theMenu.arrowDirection = UIMenuControllerArrowDown;
[theMenu setMenuVisible:YES animated:YES];

The problem is that this works fine on iOS 5 but doesn't show anything on iOS 4.3.

However on BOTH iOSes the 'UIMenuControllerWillShowMenuNotification' and the 'UIMenuControllerDidShowMenuNotification' notifications are received as expected.

Can anyone help or point me to the right direction please. Thanks in advance.


Solution

  •  if(![self becomeFirstResponder]; )
     {
        NSLog(@"Couldn't become first responder ");
        return;
     }
    

    check becomeFirstResponder first.