Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 所选文本/字符串未触发_Iphone_Ios_String_Menuitem - Fatal编程技术网

Iphone 所选文本/字符串未触发

Iphone 所选文本/字符串未触发,iphone,ios,string,menuitem,Iphone,Ios,String,Menuitem,我使用UIMenuController显示维基百科、字典和突出显示文本。我做了以下几件事 - (void)viewDidLoad { UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress)]; longPressGestureRecognizer.m

我使用
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()"];

}

我可以访问维基百科网站,但不能使用选定的文本/单词。上面的代码有什么问题?提前感谢。

执行此行时打印的内容
NSLog(@“selectedText Wiki:%@,[selectedText])?它只打印“selectedText Wiki:”。而不是所选单词。如果webView为空,是否签入selectedText?因为你的代码很好,我看不出有什么问题!我做了这个NSLog(@“Text:%@,[webView stringByEvaluatingJavaScriptFromString:@“window.getSelection().toString()”);但这也没有显示我选择的词。它只显示“文本”。我在某个地方出错了这是什么?你能在网络视图中选择一些文本吗?