摘要:我所要做的就是拥有一个UIControl子类,它在按下UIMenu时显示一个UIMenu,并且我已经阅读了这个,但是当将contextMenuinteractionEnabled属性设置为YES时,我遇到了崩溃。
类似问题:
我有一个UIControl子类,我希望向它添加一个菜单,并在单击控件时显示UIMenu。但是,当将contextMenuInteractionEnabled属性设置为YES时,我一直会收到一个错误。
下面是控件子类:
@interface MyControl : UIControl
@end
@implementation MyControl
@end它只是一个普通的UIControl子类。然后,创建该类的一个实例,并将contextMenuInteractionEnabled的值设置为YES,如下所示:
MyControl *myControl = [[MyControl alloc] init];
myControl.contextMenuInteractionEnabled = YES; //<-- Thread 1: "Invalid parameter not satisfying: interaction"但是,在运行该程序时,我会得到以下错误消息:
错误消息
* -MyControl addInteraction:,UIView.m:17965中的断言失败
*终止应用程序,原因:“无效参数不满足:交互”错误: NSInternalInconsistencyException
原因:参数不令人满意:交互
堆栈跟踪:(
0 CoreFoundation 0x00000001b8181e94 5 CDC5D9A-E506-3740-B64E-BB30867B4F1B+ 40596
1 libobjc.A.dylib 0x00000001b14b78d8 objc_exception_throw + 60
2基金会0x00000001b2a5b4c C431ACB6-FE04-3D28-B 677-4DE6E1C7D81F+ 5528396
3 UIKitCore 0x00000001ba47e5bc 179501B6-0FC2-344A-B 969-B4E3961EBE10+ 1349052
4 UIKitCore 0x00000001ba47ea70 179501B6-0FC2-344A-B969-B4E3961EBE10 + 1350256
)
libc++abi:以NSException类型的未明确例外结束
*终止应用程序,原因:“无效参数不令人满意:交互”终止,而类型为NSException的未正确异常终止
问题
顺便提一句,这段代码对于UIButton实例很好,所以UIButton类正在做一些我没有做的内部工作,但我不知道是什么。
更新1
在@DonMag的回答之后,我尝试了这样的方法:
MyControl *control = [[MyControl alloc] init];
control.showsMenuAsPrimaryAction = YES;
UIContextMenuInteraction *contextMenuInteraction = [[UIContextMenuInteraction alloc] initWithDelegate:self];
[control addInteraction:contextMenuInteraction];
[self.view addSubview:control];它不再崩溃,如果我长时间按它,菜单甚至会显示出来。但我希望它能像普通菜单一样显示出来,作为按下控件的主要动作。我要做的是在UIButton上模拟UIButton属性。这将是最理想的事情是,如果我可以实现一个控件,它有一个菜单属性,然后让该菜单作为主要操作可用。
发布于 2022-11-21 16:25:53
没必要打电话给.contextMenuInteractionEnabled = YES; ..。
下面是一个快速、完整的示例:
#import <UIKit/UIKit.h>
@interface MyControl : UIControl
@end
@implementation MyControl
@end
@interface ControlMenuViewController : UIViewController <UIContextMenuInteractionDelegate>
@end
@interface ControlMenuViewController ()
@end
@implementation ControlMenuViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyControl *myControl = [MyControl new];
UIContextMenuInteraction *interaction = [[UIContextMenuInteraction alloc] initWithDelegate:self];
[myControl addInteraction:interaction];
myControl.frame = CGRectMake(100, 200, 200, 50);
myControl.backgroundColor = UIColor.systemRedColor;
[self.view addSubview:myControl];
}
- (nullable UIContextMenuConfiguration *)contextMenuInteraction:(nonnull UIContextMenuInteraction *)interaction configurationForMenuAtLocation:(CGPoint)location {
UIContextMenuConfiguration* config = [UIContextMenuConfiguration configurationWithIdentifier:nil
previewProvider:nil
actionProvider:^UIMenu* _Nullable(NSArray<UIMenuElement*>* _Nonnull suggestedActions) {
NSMutableArray* actions = [[NSMutableArray alloc] init];
[actions addObject:[UIAction actionWithTitle:@"Stand!" image:[UIImage systemImageNamed:@"figure.stand"] identifier:nil handler:^(__kindof UIAction* _Nonnull action) {
NSLog(@"Stand!");
//[self performMenuCommandStand];
}]];
[actions addObject:[UIAction actionWithTitle:@"Walk!" image:[UIImage systemImageNamed:@"figure.walk"] identifier:nil handler:^(__kindof UIAction* _Nonnull action) {
NSLog(@"Walk!");
//[self performMenuCommandWalk];
}]];
[actions addObject:[UIAction actionWithTitle:@"Run!" image:[UIImage systemImageNamed:@"figure.run"] identifier:nil handler:^(__kindof UIAction* _Nonnull action) {
NSLog(@"Run!");
//[self performMenuCommandRun];
}]];
UIMenu* menu = [UIMenu menuWithTitle:@"" children:actions];
return menu;
}];
return config;
}
@end编辑
稍作修改,允许单龙头(主要操作):
#import <UIKit/UIKit.h>
@interface MyControl : UIControl
@property (strong, nonatomic) UIContextMenuConfiguration *menuCfg;
@end
@implementation MyControl
- (UIContextMenuConfiguration *)contextMenuInteraction:(UIContextMenuInteraction *)interaction configurationForMenuAtLocation:(CGPoint)location {
return self.menuCfg;
}
@end
@interface ControlMenuViewController : UIViewController
@end
@interface ControlMenuViewController ()
@end
@implementation ControlMenuViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyControl *myControl = [MyControl new];
myControl.frame = CGRectMake(100, 200, 200, 50);
myControl.backgroundColor = UIColor.systemRedColor;
[self.view addSubview:myControl];
// menu configuration
UIContextMenuConfiguration* config = [UIContextMenuConfiguration configurationWithIdentifier:nil
previewProvider:nil
actionProvider:^UIMenu* _Nullable(NSArray<UIMenuElement*>* _Nonnull suggestedActions) {
NSMutableArray* actions = [[NSMutableArray alloc] init];
[actions addObject:[UIAction actionWithTitle:@"Stand!" image:[UIImage systemImageNamed:@"figure.stand"] identifier:nil handler:^(__kindof UIAction* _Nonnull action) {
NSLog(@"Stand!");
//[self performMenuCommandStand];
}]];
[actions addObject:[UIAction actionWithTitle:@"Walk!" image:[UIImage systemImageNamed:@"figure.walk"] identifier:nil handler:^(__kindof UIAction* _Nonnull action) {
NSLog(@"Walk!");
//[self performMenuCommandWalk];
}]];
[actions addObject:[UIAction actionWithTitle:@"Run!" image:[UIImage systemImageNamed:@"figure.run"] identifier:nil handler:^(__kindof UIAction* _Nonnull action) {
NSLog(@"Run!");
//[self performMenuCommandRun];
}]];
UIMenu* menu = [UIMenu menuWithTitle:@"" children:actions];
return menu;
}];
// set custom control menu configuration
myControl.menuCfg = config;
// show menu on single-tap (instead of long-press)
[myControl setContextMenuInteractionEnabled:YES];
myControl.showsMenuAsPrimaryAction = YES;
}
@endhttps://stackoverflow.com/questions/74515754
复制相似问题