2009-10-07 55 views

回答

16

您可以使用UIMenuController class手動顯示剪切/複製/粘貼菜單。例如,下面的代碼將顯示菜單,集中在圖像上:

[self becomeFirstResponder]; 

UIMenuController *copyMenuController = [UIMenuController sharedMenuController]; 

[copyMenuController setTargetRect:image.frame inView:self.view]; 
[copyMenuController setMenuVisible:YES animated:YES]; 

這是假定你會爲承載您的圖像視圖可以實現在UIViewController此代碼。

爲了能夠使用各種菜單項,你還需要實現控制器幾委託方法:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{ 
    if (action == @selector(cut:)) 
     return NO; 
    else if (action == @selector(copy:)) 
     return YES; 
    else if (action == @selector(paste:)) 
     return NO; 
    else if (action == @selector(select:) || action == @selector(selectAll:)) 
     return NO; 
    else 
     return [super canPerformAction:action withSender:sender]; 
} 

- (BOOL)canBecomeFirstResponder 
{ 
    return YES; 
} 

在這種情況下,只有複製菜單選項將被啓用。您還需要實施相應的-copy:方法來處理用戶選擇該菜單項時發生的情況。