我会承认,这里已经有一个关于SO的问题,但它缺少实现细节,工作答案,我想要更多具体,所以我认为一个新的问题是为了。显然,让我知道如果我错了,我们可以尝试restart the thread over there。在自定义视图/ uiview子类上实现iphone的复制/粘贴控件
基本上,我想在用户按下标签时将UILabel中的文本复制到粘贴板。说实话,不难做到。但是,我认为提供视觉反馈的最佳方式是使用UIMenuController
中的复制菜单选项提示用户。
根据iPhone应用程序编程指南的事件处理部分,特别是关于Copy, Cut, and Paste Operations的部分,应该可以从自定义视图中提供复制,剪切和/或粘贴操作。
因此,我已经按照指南所述的以下实现对UILabel进行了分类,但UIMenuController不会显示出来。还有那还有别的要求做这种事的指南中没有任何迹象表明,和的NSLog声明并打印到控制台,指示正在执行的选择时,我按住标签:
//
// CopyLabel.m
// HoldEm
//
// Created by Billy Gray on 1/20/10.
// Copyright 2010 Zetetic LLC. All rights reserved.
//
#import "CopyLabel.h"
@implementation CopyLabel
- (void)showCopyMenu {
NSLog(@"I'm tryin' Ringo, I'm tryin' reeeeal hard.");
// bring up editing menu.
UIMenuController *theMenu = [UIMenuController sharedMenuController];
// do i even need to show a selection? There's really no point for my implementation...
// doing it any way to see if it helps the "not showing up" problem...
CGRect selectionRect = [self frame];
[theMenu setTargetRect:selectionRect inView:self];
[theMenu setMenuVisible:YES animated:YES]; // <-- doesn't show up...
}
// obviously, important to provide this, but whether it's here or not doesn't seem
// to change the fact that the UIMenuController view is not showing up
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
BOOL answer = NO;
if (action == @selector(copy:))
answer = YES;
return answer;
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self performSelector:@selector(showCopyMenu) withObject:nil afterDelay:0.8f];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showCopyMenu) object:nil];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showCopyMenu) object:nil];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showCopyMenu) object:nil];
}
@end
所以,为了做到这一点,人们还需要做些什么?
对于那些沿以下,并试图做到这一点,同样,你也需要设置“用户交互启用”作为标签
编辑:
为了清楚起见,让我补充一点,这应该与在某些iPhone视图中按住它时显示在图像上的小[复制]菜单项类似。 -B
一旦我得到了这个工作,我将一个教程和示例项目放在Mobile Orchard上:http://www.mobileorchard.com/hold-and-copy-in-uikit/ – 2010-02-25 15:37:21
教程正是我需要的,但你忘了添加“重置”方法代码。谢谢! – AlBeebe 2011-07-31 06:16:46