简体   繁体   English

UIResponder的繁琐复制方法

[英]Swizzling Copy method of UIResponder

I am swizzling Copy: and Paste: method of UIResponder. 我烦恼UIResponder的Copy:和Paste:方法。 I have to write the copied content to a private pasteboard. 我必须将复制的内容写入专用粘贴板。

- (void)copyToPrivatePasteboard:(id)sender
{
    UIPasteboard *privatePasteboard = [self getPrivatePasteboard];
    [privatePasteboard setString:@""];//How to get the copied string to store in pasteboard.
}

How can i write copied string to pasteboard. 如何将复制的字符串写到粘贴板。 The parameter i am getting is of type id. 我得到的参数是id类型。 If i convert it to NSString, it won't be proper because it is the sender who is calling this method (UIMenuController). 如果我将其转换为NSString,那将是不合适的,因为调用此方法的是发送方(UIMenuController)。

If I understand you correct, you want to replace UIResponder's -copy: with the code in you Q. (And doing the same thing with -paste: .) 如果我理解的正确,您想用Q中的代码替换UIResponder's -copy:并使用-paste:做同样的事情)。

This is the answer to your Q. But there are some comments at the end: 这是对您的问的答案。但是最后有一些评论:

  • If you swizzle a method, self will point to the receiver of the message. 如果您选择一种方法,则self将指向消息的接收者。 In your case it is the instance of UIResponder . 在您的情况下,它是UIResponder的实例。 So you have to grab the data using self . 因此,您必须使用self来获取数据。 How can you do this? 你该怎么做? Not in general, because: 一般而言,因为:

  • -copy: can contain more than "Put my data to the pasteboard." -copy:可以包含“将我的数据放入粘贴板”以外的内容。 It is a method. 这是一种方法。 The code there can deal with other things you cannot not know. 那里的代码可以处理您不知道的其他事情。 So you cannot substitute it that easy. 因此,您无法轻松替代它。

  • Because of this -copy: is typically overwritten in subclasses. 因此, -copy:通常在子类中被覆盖。 (I think, that it does nothing in its UIResponder implementation.) Swizzling -copy: in UIResponder will not change any behaviour in an instance of a subclass. (我认为,它在UIResponder实现中不执行任何操作。)Swizzling -copy:UIResponder不会更改子类实例中的任何行为。 Are you familiar with the concept of the responder chain ? 您熟悉响应者链的概念吗?

Probably there are better ways to accomplish your goal: 可能有更好的方法可以实现您的目标:

  • If you really want to do some swizzling, because it is cool, I would prefer +generalPasteboard ( UIPasteboard ). 如果您真的想做些麻烦的事,因为它很酷,那么我更喜欢+generalPasteboardUIPasteboard )。

  • Butmaybe you should describe your final goal with more details. 但是也许您应该用更多细节描述最终目标。 Probably there is a completly different way to reach it. 可能有完全不同的方式来达到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM