当用户长按分组UITableView中的单元格时,我正在尝试显示自定义UIMenuController。但是,在成功检测到长按后,我似乎无法显示UIMenuController。任何帮助是极大的赞赏。
MyViewController.h
@interface MyViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
UITableView *table;
@property (nonatomic, retain) IBOutlet UITableView *table;
@end
在cellForRowAtIndexPath中,我附上了Long Press Gesture Recognizer
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SectionsTableIdentifier] autorelease];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[cell addGestureRecognizer:longPress];
[longPress release];
这是我的handleLongPress操作方法
-(void)handleLongPress:(UIGestureRecognizer *)longPress {
if (longPress.state == UIGestureRecognizerStateBegan) {
CGPoint pressLocation = [longPress locationInView:self.table];
NSIndexPath *pressedIndexPath = [self.table indexPathForRowAtPoint:pressLocation];
UIMenuItem *first = [[UIMenuItem alloc] initWithTitle:@"Save" action:@selector(saveRecent)];
UIMenuItem *second = [[UIMenuItem alloc] initWithTitle:@"Edit" action:@selector(editQuery)];
UIMenuController *menuController = [UIMenuController sharedMenuController];
menuController.menuItems = [NSArray arrayWithObjects:first,second,nil];
[menuController setTargetRect:longPress.view.frame inView:longPress.view.superview];
[menuController setMenuVisible:YES animated:YES];
[pressedIndexPath release];
}
}
编辑和保存的Action方法只显示UIAlertView。我还实现了以下方法,以确保在显示UIMenuController时,仅存在“保存”和“编辑”选项
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
BOOL canPerform = NO;
if (action == @selector(saveRecent)) {
canPerform = YES;
}
if (action == @selector(editQuery)) {
canPerform = YES;
}
return canPerform;
}
我也声称MyViewController是第一响应者
-(BOOL)canBecomeFirstResponder {
return YES;
}
答案 0 :(得分:1)
我相信您需要拥有一个声明firstResponder状态的视图才能呈现UIMenuController。我没有在你的代码中看到这种情况。
我写了使用UIMenuController作为这个问题的答案的指示: