所选文本/字符串未触发

时间:2012-05-24 08:38:17

标签: iphone ios string menuitem

使用UIMenuController显示维基百科,词典和突出显示文本。我做了以下,

- (void)viewDidLoad
{
  UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress)];
longPressGestureRecognizer.minimumPressDuration =0.4;
longPressGestureRecognizer.delegate=self;
[webView addGestureRecognizer:longPressGestureRecognizer];
[longPressGestureRecognizer release];
}

- (void)handleLongPress//:(id)sender 
{
//location = [recognizer locationInView:self.view];
//  
//  NSLog(@"location :%d",location);

UIMenuController *menuController = [UIMenuController sharedMenuController];

[self becomeFirstResponder];

UIMenuItem *item = [[[UIMenuItem alloc] initWithTitle: @"Wikipedia"
                                               action: @selector(wiki:)] autorelease];
UIMenuItem *item1 = [[[UIMenuItem alloc] initWithTitle: @"Dictionary"
                                                action: @selector(dict:)] autorelease];
UIMenuItem *item2 = [[[UIMenuItem alloc] initWithTitle: @"Highlight"
                                                action: @selector(highlighting:)] autorelease];

[menuController setTargetRect:CGRectMake(100, 100, 320, 100) inView:webView];
[menuController setMenuVisible:YES animated:YES];
[menuController setMenuItems: [NSArray arrayWithObjects: item,item1,item2,nil]];
}

-(BOOL)canBecomeFirstResponder
{
return YES;

}

- (BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
   if (action == @selector( wiki:))
   {
    return YES; // logic here for context menu show/hide
   }

   if (action == @selector( dict:))
   {
    return YES;  // logic here for context menu show/hide
   }

  if (action == @selector(highlighting:))
  {
    // turn off copy: if you like:
    return YES;
  }
  return [super canPerformAction: action withSender: sender];
}

- (void)wiki: (id) sender
{ 
NSString *shareUrlString = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/%@",[self selectedText]];

NSLog(@"selectedText Wiki: %@",[self selectedText]);

//Create the URL string which will tell Facebook you want to share that specific page 

NSURL *url = [ [ NSURL alloc ] initWithString:shareUrlString ];
//Create the URL object 

[[UIApplication sharedApplication] openURL:url];
//Launch Safari with the URL you created
[url release];

}
- (NSString *)selectedText 
 {

  return [webView stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];

}

能够获取维基百科网站但不能使用所选文本/单词。上面的代码有什么问题?提前谢谢。

0 个答案:

没有答案