ipad - 在 iPad 中自定义选择、复制/粘贴

标签 ipad

我正在iPad上开发电子书应用程序,并使用UIMenuController。我想自定义菜单 Controller 中默认的选择、复制和粘贴功能。我无法自定义该功能请帮助我解决源代码

谢谢 库纳尔

最佳答案

派生 UITextView 如下。

//MyUITextView.h
#import <Foundation/Foundation.h>
@interface MyUITextView : UITextView {

}
@end

//MyUITextView.m
#import "MyUITextView.h"
@implementation MyUITextView
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{
    if (action == @selector(menu1:))
        return YES;

    if (action == @selector(menu2:))
        return YES;

    return NO;//[super canPerformAction:action withSender:sender];
}

- (void)menu1:(id)sender
{
    NSLog(@"in menu1");
}

- (void)menu2:(id)sender
{
    NSLog(@"in menu2");
}

@end

在 View Controller 中的 viewDidLoad 下

UIMenuItem *menu1 = [[[UIMenuItem alloc] init]autorelease];
menu1.title = @"MyMenu1";
menu1.action = @selector(menu1:);

UIMenuItem *menu2 = [[[UIMenuItem alloc] init]autorelease];
menu2.title = @"MyMenu2";
menu2.action = @selector(menu2:);

UIMenuController* mc = [UIMenuController sharedMenuController];
mc.menuItems = [NSArray arrayWithObjects: menu1, menu2, nil];

如果 TextView 位于 Nib 上,则需要从 InterfaceBuilder 为 TextView 分配类,并且如果动态创建它,则创建 MyUITextView 而不是 UITextView 的对象。

上面的代码在您选择文本时添加了 2 个自定义菜单,当您选择选项时,它将相应地触发选择器。

希望有帮助。

关于ipad - 在 iPad 中自定义选择、复制/粘贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6521368/

相关文章:

ios - 光标的焦点不会转移到 ios 中的下一个 UITextView

iphone - 从文件中读取内容

iphone - 调用方式 - (CGFloat)tableView :(UITableView *)tableView heightForFooterInSection:(NSInteger)section without reloading

iphone - 我的 iPad 应用程序需要更多内存

ios - 在 iPad 上显示一个东西,在 Apple TV 上显示另一个?

iphone - 如何在我的 iPhone 应用程序中使用日历?

iphone - 您是否有任何理由不想在每次构建 iOS 应用程序时都使用静态分析器?

iphone - 两个 View UIView 和 UIImageView 的 GestureRecognizer 问题

iphone - 如何在 UIImageView 上制作可点击的 UIButton?

ios - iPhone 上的 LandScape 方向无法正常工作