0

我有两个 UIViewController,一个是主要的,通过这个按钮可以转到第二个。在 SecondView.m 我有以下代码:

    - (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;
}

Button 链接为“内部修饰”,但是当我运行 UIMenuController 时没有显示。完全相同的代码在主 UIViewController 中工作。

谢谢

4

1 回答 1

0

如果我没有遗漏任何东西,我认为您应该,例如,将您的 sharedController.view 作为子视图添加到您的 mainController.view,例如(假设在您的主控制器中定义了 `showpopup):

- (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];
}

或者您可以模态显示您的 sharedController (替换addSubview上面的行):

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

无论如何,在我看来,“呈现”位丢失了。

于 2013-01-08T19:14:16.590 回答