ios - Swift 中的响应链(UIButton 目标中的 nil 目标)

标签 ios swift uibutton nsresponder

我在 swift 中使用响应链时遇到问题。

当我使用 nil 目标设置按钮目标时:

someButton.addTarget(nil, action:"addButtonTapped:", forControlEvents: .TouchUpInside)

Action 将被发送到响应者链,直到 Action 在 Controller 中被处理。到目前为止一切顺利:-)

但我想拦截 Action ,执行一些代码并将其转发给 Controller 。但是我找不到快速执行此操作的方法。在 ObjC 中,这个任务很容易完成,所以我想也应该有一种快速的方法。

在此先感谢您的帮助:-)

最佳答案

我的一位同事提示我重新创建选择器并再次手动发送它。

let selector = Selector("someButtonTapped:")
let target: AnyObject? = self.nextResponder()?.targetForAction(selector, withSender: button)
UIApplication.sharedApplication().sendAction(selector, to: target, from: self, forEvent: nil)

这将重新创建响应者链并将新消息转发给下一个响应者。

我希望有人会觉得这很有用。

关于ios - Swift 中的响应链(UIButton 目标中的 nil 目标),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29815240/

相关文章:

ios - 在 iOS 中 - 生产和暂存服务器包含在单个构建中

swift - 在自定义单元格中使用 UISwitch 设置对象属性

ios - UIButton:将图像放置在标题旁边

ios - 内容插图不工作 Storyboard Xcode8

ios - 仅将圆角半径设置为 UIView/UIButton 的特定一侧

ios - 尽管没有编译器警告,但 Cocos2D v2.1 和 arm64 在 iPad Air 上崩溃

ios - IOS 5.1 中的方向问题?

文档中的 Swift 方法参数

ios - 获取当前界面方向的不同方式?

ios - 在 Xcode 中删除类文件导致编译错误 : "<unknown>:0: error: no such file or directory"