[英]Reload WebView from Menu Item (OS X)
I want to reload a WebView
whenever a certain menu item is clicked. 我想在每次单击某个菜单项时重新加载
WebView
。 Here's my code that's not working. 这是我的代码不起作用。 (Assume normal declarations in
.h
files.) (假定
.h
文件中的普通声明。)
// AppDelegate.m
#import "AppDelegate.h"
// #import "ViewController.h" (in AppDelegate.h)
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
// Menu Item triggers this:
- (IBAction)reloadWebView:(id)sender {
NSLog(@"AppDelegate reloadWebView");
ViewController * vc = [[ViewController alloc] init];
[vc reloadWebView:sender]; // tried this,
[vc.webview reload:sender]; // and this,
[[vc.webview mainFrame] reload]; // and this,
[[vc.webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/"]]]; // and this.
}
@end
// ViewController.m
#import "ViewController.h"
// @property (assign) IBOutlet WebView *webview; (in ViewController.h)
@implementation ViewController
@synthesize webview;
- (void)viewDidLoad {
[super viewDidLoad];
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/"]]]; // this works
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
// Called in AppDelegate:
- (void)reloadWebView:(id)sender {
NSLog(@"ViewController reloadWebView");
// The problem here seems to be: webview == nil
[webview reload:sender]; // also tried this,
[[webview mainFrame] reload]; // and this,
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/"]]]; // and this.
}
@end
This is all my code. 这就是我的全部代码。 I've just started working on this project.
我刚刚开始从事这个项目。
I've tried using a button in the ViewController
. 我试过使用
ViewController
的按钮。 That works, but I need this to be a menu item. 那行得通,但我需要将此作为菜单项。
Thanks in advance! 提前致谢!
instead of creating a new view controller: 而不是创建新的视图控制器:
ViewController * vc = [[ViewController alloc] init];
try: 尝试:
ViewController * vc = (ViewController *)[[[NSApplication sharedApplication] mainWindow] contentViewController];
to call the current view controller 调用当前的视图控制器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.