I have 2 viewControllers and in first one I'm using tapRecognizer to press and hold in order to show a UImenucontroller to copy a string. The tap is used for selecting the title on navigation bar, and it shows a UImenucontroller with copy item on it.
It works for the first time, but when user switch to another view controller and come back to the first view controller again, the menu does not show any more.
-(void)viewDidLoad{
[super viewDidLoad];
UIView *viewWithTitleLabel = self.navigationController.navigationBar.subviews[1];
viewWithTitleLabel.userInteractionEnabled = YES;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(topBarTitleTap:)];
[viewWithTitleLabel addGestureRecognizer:longPress];
}
-(void)topBarTitleTap:(UILongPressGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
UIMenuController *menuController = [UIMenuController sharedMenuController];
[menuController setTargetRect:CGRectMake(CGRectGetMidX([self.view bounds]), -12.0, 0.0f, 0.0f) inView:self.view];
[menuController setMenuVisible:YES animated:YES];
}
}
- (void) copy:(id) sender {
// called when copy clicked in tab bar title
NSString *copyStringverse = self.navigationItem.title;
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:copyStringverse];
}
- (BOOL) canBecomeFirstResponder {
return YES;
}