在Cocoa,UIResponder中从子视图发送消息到超级视图?

发布于 2024-10-25 05:58:33 字数 158 浏览 4 评论 0原文

我有所有这些触摸敏感的子视图,我想从子视图向超级视图发送一条消息,说明用户选择了它,以便超级视图可以与控制器的其余部分进行通信。

我无法在子视图和控制器之间进行通信, 子视图>>超级视图>>控制器

也许使用 UIResponder 来实现这一点?

I have all these subview which are touch sensitive, I want to send a message from the subview to the superview, to say that a user selected it, so the superview can communicate with the rest of the controller.

I can not communicate between the subviews and the controllers,
subviews >> superview >> controller

Perhaps use, UIResponder to achieve this?

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

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

发布评论

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

评论(2

柠檬色的秋千 2024-11-01 05:58:33

根据子视图,目标/操作机制可能对此很有用。如果您可以从 UIControl 派生子视图,那么让您的子视图将其操作消息发送到其目标(通常是您的视图控制器)就特别容易。您将能够在 Interface Builder 中布置视图,并通过将它们连接到视图控制器来指定它们的目标和操作。如果由于某种原因无法从 UIControl 派生它们,那么您必须实现目标/操作的等效项,并且在 IB 中不会获得相同的支持,但它仍然非常简单。

另一种可能性是让视图控制器对所有子视图进行触摸处理。正如您所建议的,这基本上是利用响应者链,但是是在触摸处理级别。如果有很多子视图需要跟踪,这可能并不理想,但它是可行的。

第三种方法是让您的子视图在被选择时发布通知。

事实上,UIResponder 不提供沿着响应者链传递任意消息的机制。我不确定添加该功能是否是专门从子视图向视图控制器发送消息的最优雅的方式。视图控制器和子视图之间可能存在许多中间对象,当您已经知道消息要发送到哪里时,涉及整个链似乎是错误的。然而,考虑扩展 UIResponder 以使响应者链成为不仅仅是事件的渠道是很有趣的。您可以向 UIResponder 添加一个类别:

@interface UIResponder (Messages)
- (void)sendMessage:(SEL)message withObject:(id)object;
@end;

@implementation UIResponder (Messages)
- (void)sendMessage:(SEL)message withObject:(id)object
{
    if ([self respondsToSelector:message]) {
        [self performSelector:message withObject:object];
    }
    else {
        [[self nextResponder] sendMessage:message withObject:object];
    }
}
@end

警告 上面的代码完全未经测试,并且由于我尚未想到的原因,可能是一个糟糕的想法。谨慎行事。预计会出现编译错误。交叉手指。如果效果很好请告诉我,如果效果不好请别打扰我。

Depending on the subviews, the target/action mechanism can be good for this. If you can derive your subviews from UIControl, then it's particularly easy to have your subviews send their action message to their target, which is usually your view controller. You'll be able to lay out your views in Interface Builder and specify their target and action by connecting them to the view controller. If you can't derive them from UIControl for some reason, then you'd have to implement the equivalent of target/action and you won't have the same support in IB, but it's still pretty straightforward.

Another possibility is to let the view controller do the touch handling for all the subviews. This is basically taking advantage of the responder chain as you suggested, but at the touch-handling level. It may not be ideal if there are a lot of subviews to keep track of, but it's workable.

A third way to do it is to have your subviews post a notification when they're selected.

As is, UIResponder doesn't provide a mechanism for passing arbitrary messages along the responder chain. I'm not sure that adding that capability is the most elegant way to send a message specifically from a subview to the view controller. There are potentially many intermediate objects between the view controller and the subviews, and involving the entire chain when you already know where you want the message to go seems wrong. However, it's interesting to think about extending UIResponder to make the responder chain a conduit for more than just events. You could add a category to UIResponder:

@interface UIResponder (Messages)
- (void)sendMessage:(SEL)message withObject:(id)object;
@end;

@implementation UIResponder (Messages)
- (void)sendMessage:(SEL)message withObject:(id)object
{
    if ([self respondsToSelector:message]) {
        [self performSelector:message withObject:object];
    }
    else {
        [[self nextResponder] sendMessage:message withObject:object];
    }
}
@end

WARNING The code above is completely untested, and may be a lousy idea for reasons I haven't yet thought of. Proceed with caution. Expect compilation errors. Cross your fingers. Please let me know if it works out well, and leave me alone if it doesnt.

微凉 2024-11-01 05:58:33

为什么不能使用[self.superview sendMessage]

Why can't you use [self.superview sendMessage]?

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