以下是一個完整的評論工作示例...
查看子類的頭文件
#import <Foundation/Foundation.h>
@interface MenuControllerSupportingView : UIView
{
}
@end
查看子類的源文件
#import "MenuControllerSupportingView.h"
@implementation MenuControllerSupportingView
//It's mandatory and it has to return YES then only u can show menu items..
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(void)MenuItemAClicked
{
NSLog(@"Menu item A clicked");
}
-(void)MenuItemBClicked
{
NSLog(@"Menu item B clicked");
}
-(void)MenuItemCClicked
{
NSLog(@"Menu item C clicked");
}
//It's not mandatory for custom menu items
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if(action == @selector(MenuItemAClicked))
return YES;
else if(action == @selector(MenuItemBClicked))
return YES;
else if(action == @selector(MenuItemCClicked))
return YES;
else
return NO;
}
視圖控制器頭文件
#import <UIKit/UIKit.h>
@interface ViewController1 : UIViewController
@end
視圖控制器源碼文件
#import "ViewController1.h"
#import "MenuControllerSupportingView.h"
@interface ViewController1()
{
MenuControllerSupportingView *vu;
}
@end
@implementation ViewController1
- (void)viewDidLoad
{
[super viewDidLoad];
vu=[[SGGI_MenuControllerSupportingView alloc]initWithFrame:CGRectMake(0,0,768,1024)];
[self.view addSubview:vu];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(200,200,200,30)];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn setTitle:@"Show" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(SHowMenu) forControlEvents:UIControlEventTouchUpInside];
[vu addSubview:btn];
}
-(void)SHowMenu
{
UIMenuController *menucontroller=[UIMenuController sharedMenuController];
UIMenuItem *MenuitemA=[[UIMenuItem alloc] initWithTitle:@"A" action:@selector(MenuItemAClicked)];
UIMenuItem *MenuitemB=[[UIMenuItem alloc] initWithTitle:@"B" action:@selector(MenuItemBClicked)];
UIMenuItem *MenuitemC=[[UIMenuItem alloc] initWithTitle:@"C" action:@selector(MenuItemCClicked)];
[menucontroller setMenuItems:[NSArray arrayWithObjects:MenuitemA,MenuitemB,MenuitemC,nil]];
//It's mandatory
[vu becomeFirstResponder];
//It's also mandatory ...remeber we've added a mehod on view class
if([vu canBecomeFirstResponder])
{
[menucontroller setTargetRect:CGRectMake(10,10, 0, 200) inView:vu];
[menucontroller setMenuVisible:YES animated:YES];
}
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
在View類如果u寫canPerformAction是獨自返回你會看到所有默認的菜單項類似相機的象徵,剪切,複製等。
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
return YES;
}
如果u想獨自顯示類似相機然後
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if([email protected](_insertImage:))
return YES;
else
return NO;
}
如果u想知道,那麼所有的行動
參觀link
樂於助人,非常有幫助。 – Jake 2011-09-06 15:03:06
這在MonoTouch中的效果非常好 - 只有要注意的事情是'CanBecomeFirstResponder'是一個屬性重寫,並且您必須爲'UIMenuController.MenuItems'屬性指定一個'UIMenuItem []'數組。 – PaulJ 2012-05-10 08:45:58
當我在視圖*上調用'becomeFirstResponder' *時沒有工作。在控制器上調用它工作得很好。 – 2013-04-15 17:02:26