【发布时间】:2014-11-14 01:28:06
【问题描述】:
我在 Xcode 5 中有一个可用的 .xib。主要小部件是 UIWebView。小部件的位置、连接和插座都正确排列。
后来我创建了一个 UIWebView 的 Category (UIWebView+ReadOnlyPageContent) 来覆盖canPerformAction:withSender: 方法。
➥ 有没有办法将小部件的类重新分配给类别?
在 Xcode 的身份检查器 > 自定义类 > 类字段中,我看到“UIWebView”。但是文本是灰色的,弹出菜单中只列出了“UIWebView”。
在我的视图控制器中,我尝试重新定义 UIWebView 小部件,从这里开始:
IBOutlet UIWebView *webView; // Works.
到我的类别,带有#import 的类别:
IBOutlet UIWebView+ReadOnlyPageContent *webView; // Fails. 3 compiler errors.
作为替代方案,我考虑过创建 UIWebView 的子类。但是文档说 UIWebView 不应该被子类化。所以使用类别是我能想到的唯一方法来覆盖canPerformAction:withSender: 方法。
- (BOOL)canPerformAction:(SEL)action
withSender:(id)sender
{
if (
action == @selector(select:) ||
action == @selector(copy:) ||
action == @selector(cut:) ||
action == @selector(paste:)
)
{
return NO;
}
return [super canPerformAction:action withSender:sender];
}
【问题讨论】:
标签: objective-c interface-builder xib objective-c-category