将子视图设置为调用父 UIView 子类的响应者

发布于 2024-12-08 19:29:46 字数 354 浏览 2 评论 0原文

我有一个 UIView,我已将其子类化为整个应用程序中使用的主视图。其中,我有两个子视图:横幅和容器。横幅基本上是放置广告或免责声明或其他内容的地方。容器旨在充当主视图,您可以向其添加、删除等操作,就好像它是唯一的视图一样。

现在,我只是重写父视图的方法并将调用发送到容器视图。我想知道是否有一种更简单的方法来做到这一点,而不必为每个方法编写这样的内容:

- (void)addSubview:(UIView*)view {
  [container addSubview:view];
}

也许可以让您将对视图的所有方法调用委托给特定的子视图,而不是响应方法调用本身。

有人知道这是否可能吗?

I've got a UIView that I've subclassed to be the main view used throughout my app. In it, I have two subviews: banner and container. Banner is basically a place to put an ad or a disclaimer or whatever. Container is meant to act as the primary view, to which you can add, remove and whatever as if it were the only view.

Right now, I'm just overriding the methods of the parent view and sending the calls to the container view. I'm wondering if there is an easier way to do this, without having to write out stuff like this for every method:

- (void)addSubview:(UIView*)view {
  [container addSubview:view];
}

Maybe something that lets you delegate all method calls to the view to a specific subview, rather than responding to the method calls itself.

Anyone know if this is possible?

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

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

发布评论

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

评论(1

生死何惧 2024-12-15 19:29:46

我对这个问题有点困惑。

响应者链存在,并按层次结构将 ui 事件向上传递到屏幕上的所有可见视图。阅读一些有关响应者链的信息可能会很有用,因为根据设计,它按顺序将事件从最深的视图传递到最高的(根),这与您正在寻找的方向相反(如果我正在阅读这对)。

如果您需要将事件从超级视图转发到子视图,为了遵守封装原则,您应该在子视图的子类接口中定义适当的操作,然后配置您的超级视图以针对该子视图/类中的操作。

I'm a little confused by the question.

The responder chain is present and passes ui events up through all visible views on screen, by hierarchy. It may be useful to read a little about the responder chain, because by design it passes events from the deepest view to the highest (root) in that order, which is the opposite of the direction you're seeking (if I'm reading this right).

If you need to forward events from a superview to a subview, to respect principles of encapsulation, you should define appropriate actions in your subview's subclass interface, and then then configure your superview to target the actions in that subview/class.

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