在 IOS 应用程序中,我想在长按位于 UITableViewCell 中的 UIView 时显示 UIMenuController。
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
myTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell==nil)
{
cell=(myTableViewCell*)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
myView=<initMyView>
[cell.contentView addSubview:myView];
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTapGestureCapturedOnView:)];
longTap.minimumPressDuration=0.5f;
[myView addGestureRecognizer:longTap];
return cell;
}
- (void)longTapGestureCapturedOnView:(UITapGestureRecognizer *)gesture
{
items=[[NSMutableArray alloc]init];
if (gesture.state == UIGestureRecognizerStateBegan)
{
[items addObject:[[UIMenuItem alloc] initWithTitle:@"Copy" action:@selector(myCopy:)]];
[items addObject:[[UIMenuItem alloc] initWithTitle:@"Custom" action:@selector(myCustom:)]];
}
[[UIMenuController sharedMenuController] setMenuItems:items];
[[UIMenuController sharedMenuController] update];
[[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
BOOL result = NO;
if(@selector(myCopy:) == action ||@selector(myCustom:) == action ) {
result = YES;
}
return result;
}
被longTapGestureCapturedOnView
调用,以及canPerformAction
在 2 种情况下返回 YES,但屏幕上没有菜单项出现。我做错了什么?