当我在我的网页视图中按下向上键时,它会记录它,但它不会触发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;
}
答案 0 :(得分:0)
默认情况下,WebView
不会显示JavaScript警报。实际上,您需要通过将对象设置为WebView
的委托并实现
‑webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:
委托方法。
但是,在您的情况下,绝对没有理由这样做,因为您只需创建一个标准NSAlert
。我不明白你为什么试图让WebView
显示警报。为什么不直接显示它?