gpt4 book ai didi

ios - 自定义编辑菜单中 UIMenuItems 的顺序

转载 作者:行者123 更新时间:2023-11-28 19:07:26 25 4
gpt4 key购买 nike

我想在选择菜单中添加自己的命令,同时保留标准的“复制”、“剪切”等命令。我用这个:

    UIMenuItem *myItem = [[UIMenuItem alloc] initWithTitle:@"My Command" action:@selector(myCommand:)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects: myItem, nil]];

但这会将我的命令添加到编辑菜单中列表的最后。我希望我的命令首先出现在其中。我怎样才能做到这一点?

最佳答案

自己解决了。这是我的 initWithCoder: 方法中的内容:

    UIMenuItem *myCommandItem = [[UIMenuItem alloc] initWithTitle:@"My Command" action:@selector(myCommandPressed:)];

UIMenuItem *cutItem = [[UIMenuItem alloc] initWithTitle:@"Cut" action:@selector(myCut:)];
UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"Copy" action:@selector(myCopy:)];
UIMenuItem *pasteItem = [[UIMenuItem alloc] initWithTitle:@"Paste" action:@selector(myPaste:)];
UIMenuItem *selectItem = [[UIMenuItem alloc] initWithTitle:@"Select" action:@selector(mySelect:)];
UIMenuItem *selectAllItem = [[UIMenuItem alloc] initWithTitle:@"Select all" action:@selector(mySelectAll:)];
UIMenuItem *deleteItem = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(myDelete:)];

[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects: myCommandItem,
cutItem, copyItem, pasteItem, selectItem, selectAllItem, deleteItem, nil]];

现在这个:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {

if (action == @selector(myCommandPressed:)) {
return YES;
}

if (action == @selector(myCut:)) {
return [super canPerformAction:@selector(cut:) withSender:sender];
}

if (action == @selector(myCopy:)) {
return [super canPerformAction:@selector(copy:) withSender:sender];
}

if (action == @selector(myPaste:)) {
return [super canPerformAction:@selector(paste:) withSender:sender];
}

if (action == @selector(mySelect:)) {
return [super canPerformAction:@selector(select:) withSender:sender];
}

if (action == @selector(mySelectAll:)) {
return [super canPerformAction:@selector(selectAll:) withSender:sender];
}

if (action == @selector(myDelete:)) {
return [super canPerformAction:@selector(delete:) withSender:sender];
}

return NO;
}

最后:

- (void) myCommandPressed: (id) sender {
NSLog(@"My Command pressed");
}


- (void) myCut: (id) sender {
[self cut:sender];
}

- (void) myCopy: (id) sender {
[self copy:sender];
}

- (void) myPaste: (id) sender {
[self paste:sender];
}

- (void) mySelect: (id) sender {
[self select:sender];
}

- (void) mySelectAll: (id) sender {
[self selectAll:sender];
}

- (void) myDelete: (id) sender {
[self delete:sender];
}

关于ios - 自定义编辑菜单中 UIMenuItems 的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19056601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com