在类中指定自己的委托,在 Interface Builder 中进行类型检查

发布于 2024-12-25 23:31:02 字数 514 浏览 10 评论 0原文

我编写了一个 Objective-C 类,需要通知另一个类,因此我为委托定义了一个协议:

@protocol glob_protocol <NSObject>
  @required
  - (IBAction) call:(int) val val2:(int) val2;
@end

在我的类中,我有一个成员来存储委托:

  IBOutlet id <glob_protocol> delegate;

在 Interface Builder 中连接类/实例时,没有类型检查现在。我可以作为代表连接任何班级。

有没有办法启用类型检查?我希望只有实现该协议的类/实例才能设置为委托。

感谢您的任何提示, 托斯顿.

编辑: 我刚刚检查了 UIKit 的头文件,看看是否有任何其他信息或关键字,但我没有找到任何信息或关键字。我想知道 InterfaceBuilder 从哪里获得可以将哪些实例设置为委托的信息。

I wrote an objective-C class that needs to notify another class, so i defined a protocol for a delegate:

@protocol glob_protocol <NSObject>
  @required
  - (IBAction) call:(int) val val2:(int) val2;
@end

In my class i have a member to store the delegate:

  IBOutlet id <glob_protocol> delegate;

When connecting the classes / instances in the Interface Builder there is no type checking now. I can connect any class as a delegate.

Is there a way to enable type checking? I'd like that only classes / instances that implement the protocol can be set as delegates.

Thanks for any hints,
Torsten.

EDIT:
I just checked in the Header files of UIKit to see if there are any additional infos or keywords but i did not find any. I wonder where InterfaceBuilder got tthe information from what instances can be set as delegate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回梦 2025-01-01 23:31:02

这通常有效:

- (void)setDelegate:(id<glob_protocol>)inDelegate
{
  if (inDelegate) {
    assert([inDelegate conformsToProtocol:@protocol(glob_protocol)]);
  }
  ...
}

尽管这不是您想要的级别/阶段,但它效果很好,因为连接不会经常改变。

this usually works:

- (void)setDelegate:(id<glob_protocol>)inDelegate
{
  if (inDelegate) {
    assert([inDelegate conformsToProtocol:@protocol(glob_protocol)]);
  }
  ...
}

although that's not at the level/phase you want, it works well because connections don't change often.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文