确定响应者链是否将处理操作 [英] Determine if responder chain will handle an action

查看:146
本文介绍了确定响应者链是否将处理操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

响应者链很酷.

特别是,能够将自定义操作发送给第一响应者,该响应者会冒犯到可能感兴趣的其他任何人:[[UIApplication sharedApplication] sendAction: @selector(commandToSend) to: nil from: self forEvent: nil].

Particularly, being able to send custom actions to the first responder that will bubble up to anyone else that might be interested: [[UIApplication sharedApplication] sendAction: @selector(commandToSend) to: nil from: self forEvent: nil].

或者使用以下更少的自定义操作

Or less custom actions with:

[[UIApplication sharedApplication] sendAction: @selector(resignFirstResponder) to: nil from: self forEvent: nil]

我想知道–是否可以先行测试,如果如果现在发送,来自特定发件人的特定操作是否会得到处理?一个明显的用途是启用和禁用按钮,这些按钮根据当前是否要处理该动作来发送动作.

I'd like to know – is there a way to test before hand if a particular action from a particular sender would be handled if it were sent now? An obvious use for this would be to enable and disable buttons that send actions dependent on whether that action is going to be handled at the moment.

如果您碰巧认识第一响应者,我认为您可以执行[aResponder targetForAction: @selector(methodYouWantToSend) withSender: selfOrSomethingElse],并检查答案是否为nil.但是,似乎没有一种等效于UIApplicationsendAction:…的方法,该方法会自动从第一个响应程序开始并正常工作.

If you happen to know the first responder, I think you can do [aResponder targetForAction: @selector(methodYouWantToSend) withSender: selfOrSomethingElse], and check whether the answer is nil or not. But there doesn't seem to be a an equivalent method to UIApplication's sendAction:… that will automatically start at the first responder and work up.

推荐答案

这里有一个令人愉快的解决方案,可以找到第一个通过利用响应者链来响应者.

There's a delightful solution here to finding the first responder by exploiting the responder chain.

一旦您认识了第一响应者,就很容易会问当前的响应者链是否处理特定的操作.您甚至可以根据需要找到谁来处理它.

Once you know the first responder, it's easy to ask if the current responder chain handles a particular action. You can even find out who'll be handling it, should you wish.

const SEL action = @selector(theActionYouWantToSend);
UIResponder *const currentTarget = [[UIResponder firstResponder] targetForAction: action  withSender: self];
const bool actionIsHandled = currentTarget != nil;

这篇关于确定响应者链是否将处理操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆