我创建了一个继承自 NSView 的自定义 View 。 我的目标是当用户单击自定义 View 时通知与包含自定义 View 的窗口关联的 NSWindowControl。
我想使用 Action - Target 模式来实现这一点,就像 NSButton 一样。 这样我就可以使用 Interface Builder 将自定义 View 与窗口 Controller 中的操作连接起来。
最佳答案
将以下内容添加到您的自定义 View 头文件中:
@interface MyCustomView : NSView
@property (weak, nonatomic) id userClickedTarget;
@property (assign, nonatomic) SEL userClickedAction;
@end
在自定义 View 实现文件中合成 getter/setter(这对于最新版本的 Xcode/clang 来说实际上是可选的):
@synthesize userClickedTarget = _userClickedTarget;
@synthesize userClickedAction = _userClickedAction;
并在代码中调用目标/操作:
if (_userClickedTarget && _userClickedAction) {
[_userClickedTarget performSelector:_userClickedAction
withObject:self
afterDelay:0.0];
}
请注意,使用 performSelector:withObject:afterDelay
将调用与 View 代码解耦,并使其在下次处理 runloop 时运行。
关于objective-c - 在自定义 View 中实现目标 - 操作模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21156288/