Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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
stringByEvaluatingJavaScriptFromString不';t fire JavaScript_Javascript_Objective C_Cocoa_Webview - Fatal编程技术网

stringByEvaluatingJavaScriptFromString不';t fire JavaScript

stringByEvaluatingJavaScriptFromString不';t fire JavaScript,javascript,objective-c,cocoa,webview,Javascript,Objective C,Cocoa,Webview,当我在web视图中按向上键时,它会记录它,但不会触发stringByEvaluatingJavaScriptFromString中定义的JS警报,有什么想法吗?谢谢 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { [webView setMainFrameURL:@"http://google.com"]; [webView setAutoresizingMa

当我在web视图中按向上键时,它会记录它,但不会触发
stringByEvaluatingJavaScriptFromString
中定义的JS警报,有什么想法吗?谢谢

 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        [webView setMainFrameURL:@"http://google.com"];
        [webView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

    }

     -(id)init {
        if( !(self = [super init]) ){
            return nil;
        }

        NSEvent * (^monitorHandler)(NSEvent *);
        monitorHandler = ^NSEvent * (NSEvent * theEvent){

            switch ([theEvent keyCode]) {
                case 123:    // Left arrow
                    NSLog(@"Left behind.");
                    break;
                case 124:    // Right arrow
                    NSLog(@"Right as always!");
                    break;
                case 125:    // Down arrow
                    NSLog(@"Downward is Heavenward");
                    break;
                case 126:    // Up arrow
                    [webView stringByEvaluatingJavaScriptFromString:@"alert('yo')"];
                    NSLog(@"Up, up, and away!");
                    break;
                default:
                    break;
            }
            // Return the event, a new event, or, to stop 
            // the event from being dispatched, nil
            return theEvent;
        };

        // Creates an object we do not own, but must keep track
        // of so that it can be "removed" when we're done
        eventMon = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask 
                                                         handler:monitorHandler];

        return self;
    }

默认情况下,
WebView
不会显示JavaScript警报。事实上,您需要通过将对象设置为
WebView
的委托并实现

委托方法

然而,在您的情况下,绝对没有理由这样做,因为您可以简单地创建一个标准。我不明白你为什么试图让
WebView
显示警报。为什么不直接显示呢