How to use Menu & Paste board
As context-menu and paste function are so convenience in not only web-appli and but also self-appli with freely string send. We’d like to introduce how to implement.
- How to appear menu ?
- Event capture
Most important issue in menu functionality as how to capture the event. You can google with “touchs began”, many pages would be displayed. However, we use little trick as “keyboard show”
123456789101112131415161718192021222324252627- (void)viewWillAppear:(BOOL)animated{[super viewWillAppear:animated];self.navigationItem.title = _detailItem.name;// NSURL *url = [NSURL URLWithString:@"http://www.yahoo.co.jp"];NSURL *url = [NSURL URLWithString:_detailItem.address.loginURL];NSURLRequest *req = [NSURLRequest requestWithURL:url];[myWebView loadRequest:req];[self copyToID];// Start observingif (!_observing) {NSNotificationCenter *center;center = [NSNotificationCenter defaultCenter];[center addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];[center addObserver:selfselector:@selector(keybaordWillHide:)name:UIKeyboardWillHideNotificationobject:nil];_observing = YES;}}12345678910111213141516- (void)keyboardWillShow:(NSNotification *)notificatioin{NSMutableArray* menuItems = [NSMutableArray array];UIMenuController* menuController = [UIMenuController sharedMenuController];[menuController setTargetRect:CGRectZero inView:self.view];menuController.arrowDirection = UIMenuControllerArrowDown;[menuItems addObject:[[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"login-ID", @"") action:@selector(menu1:)]];[menuItems addObject:[[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"login-PWD", @"") action:@selector(menu2:)]];menuController.menuItems = menuItems;[menuController setMenuVisible:YES animated:YES];return;} - menu setting
Implement menu setting code. We can create context-menu in this part.
123456789- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {if (action == @selector(menu1:) ||action == @selector(menu2:) ||action == @selector(paste:)){return YES;}return [super canPerformAction:action withSender:sender];}
- Event capture
- Paste board handling
- Use findFirstResponder
findFirstResponder in this software.
1234567891011121314151617181920@implementation UIView (FindFirstResponder)- (UIView *)findFirstResponder{if (self.isFirstResponder) {return self;}for (UIView *subView in self.subviews) {UIView *firstResponder = [subView findFirstResponder];if (firstResponder != nil) {return firstResponder;}}return nil;}@end - menu setting
Detail implementation in this section. String would be sent both web-appli and self-appli.
12345678910111213141516171819- (void)menu1:(id)sender{[self copyToID];UIView* firstView = [myWebView findFirstResponder];if ([firstView respondsToSelector:@selector(paste:)]) {[firstView paste:sender];}}- (void)menu2:(id)sender{[self copyToPassword];UIView* firstView = [myWebView findFirstResponder];if ([firstView respondsToSelector:@selector(paste:)]) {[firstView paste:sender];}}
- Use findFirstResponder
- Download
You can get below link.
pwdSupporter