ios - 从包含的 UIViewController 向 UIView 的 UIButton 分配回调

标签 ios uiview uiviewcontroller uibutton

现在我有一个托管 UIView 子类的 UIViewController 子类。这个 UIView 有一个 UIButton,我想将触摸事件发送到 UIViewController 子类。现在我在 UIView 子类中有一个 getter 方法,所以我捕获按钮并通过使用 UIViewController 子类中的引用来分配它。

实现此结果的最佳方法是什么?

最佳答案

将 Action 事件添加到您的 ViewController

- (IBAction)myButtonPressed:sender;

现在,如果您正在使用 IB(您应该),只需将其链接起来,或者如果在代码中执行此操作,则手动创建事件链接。

[myButton addTarget:self action:@selector(myButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

关于ios - 从包含的 UIViewController 向 UIView 的 UIButton 分配回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35074024/

相关文章:

Objective-C 继承和类型转换

ios - UIView的shadowpath颜色没有改变

uiviewcontroller - 是否可以在 UIViewController 的子类中实现 topLayoutGuide 和 bottomLayoutGuide?

ios - 如何更新 iOS 中覆盖 supportedInterfaceOrientations 函数的旧 Swift 代码?

ios - 使用 icloud 进行本地核心数据存储

ios - 测试两个整数以确定现在是白天还是晚上

ios - 使用 MKDirections (Swift 4) 导出两点之间的行驶距离后,如何访问封闭之外的距离值?

iphone - 如何获得全尺寸的 UINavigationBar titleView

ios - 如何在 Storyboard 中的 UITableViewController 底部添加工具栏?

ios - 崩溃到新的 View Controller