You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UIControl子类点击显示UIMenu崩溃问题及实现需求

自定义UIControl点击显示UIMenu的崩溃与需求问题

摘要

我想要实现一个UIControl子类,使其在点击时显示UIMenu,参考相关问题后,设置contextMenuInteractionEnabled为YES时出现崩溃。

代码与崩溃问题

自定义UIControl子类代码

@interface MyControl : UIControl
@end

@implementation MyControl
@end

触发崩溃的代码

MyControl *myControl = [[MyControl alloc] init];
myControl.contextMenuInteractionEnabled = YES; //<-- 崩溃:Thread 1: "Invalid parameter not satisfying: interaction"

错误信息

*** Assertion failure in -[MyControl addInteraction:], UIView.m:17965
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: interaction'

错误堆栈包含CoreFoundation、libobjc.A.dylib等模块的调用信息。

疑问

  • 错误信息"Invalid parameter not satisfying: interaction"是什么意思?如何修复?
  • 我哪里操作错误?仅需实现点击自定义UIControl时显示菜单。

注:该代码在UIButton实例上可正常运行。

更新1:长按显示菜单的问题

按照方案修改代码后不再崩溃,但仅长按才显示菜单,希望实现类似UIButton的menu属性效果,点击即显示菜单:

MyControl *control = [[MyControl alloc] init];
control.showsMenuAsPrimaryAction = YES;

UIContextMenuInteraction *contextMenuInteraction = [[UIContextMenuInteraction alloc] initWithDelegate:self];
[control addInteraction:contextMenuInteraction];

[self.view addSubview:control];

内容的提问来源于stack exchange,提问作者ashipma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 06:01:00