且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在WebView(OSX项目)中启动时加载URL?

更新时间:2023-12-05 16:40:58

很难确定问题出在哪里,所以猜测一下...

Hard to be sure what the issue is here, so a guess...

您要以哪种方式在IB中拖动连接?

Which way are you dragging the connection in IB?

要连接您的出口,您要将其从Inspector中显示的出口拖动到Web视图:

To connect your outlet you want to drag from the outlet shown in the Inspector to the web view:

如果以其他方式拖动,则尝试从Web视图到大纲中的应用程序委托".

If you drag in the other way, from the web view to the App Delegate in the outline you are trying to connect an action.

您的代码中还有一个问题,您的实例变量:

You also have an issue in your code, your instance variable:

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
   WebView *myWebView;
   //other instance variables
}

您的媒体资源将不会使用它:

will not be used by your property:

@property (retain, nonatomic) IBOutlet WebView *myWebView;

,因为您的属性是自动合成的,因此将创建一个实例变量_myWebView.您应该会看到一个编译器警告这种情况.

as your property is automatically synthesised and so will create an instance variable _myWebView. You should see a compiler warning to this effect.

这反过来意味着声明:

[[myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];

不会满足您的期望,因为myWebView将是nil,而不是引用您的WebView.您应该将该属性称为self.myWebView:

will not do what you expect, as myWebView will be nil and not refer to your WebView. You should refer to the property as self.myWebView:

[[self.myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];

进行这些更改后,您应该会在网络视图中看到Google.

With those changes you should see Google in your web view.

HTH