gpt4 book ai didi

objective-c - Xcode 4.5.2 - OS X 应用程序 - 将 webview 链接到代码,加载手工编码的主页

转载 作者:行者123 更新时间:2023-12-04 05:12:05 25 4
gpt4 key购买 nike

我正在使用 Xcode 4.5.2。

  • 我创建了一个新项目(OS X > 应用程序 > Cocoa 应用程序)。
  • 在链接的框架和库下,我添加了 Webkit.framework。
  • 在 MainMenu.xib 下,我向窗口添加了一个 Web View 对象
  • 在 MainMenu.xib 下,我添加了 2 个 Push Buttons 并将它们命名为 Back 和 Forward。
  • 我控制单击按钮并将它们链接到 Web View 并选择 goBack: 和 goForward:
  • 在 MainMenu.xib 下,我添加了一个 Text Field 对象
  • 我控制单击文本字段并将其链接到 Web View 并选择 takeStringURLFrom:

  • 所有这些都奏效了。我可以在文本字段中输入网址,然后按回车键并使用后退和前进按钮。

    我想手动编写一个默认网站 ( http://www.google.com/ ) 以加载,但我无法弄清楚从这里做什么?

    我将我的 Web View 链接到什么(当我控制单击它时)?

    我需要在 AppDelgate.h 和 AppDelgate.m 文件中添加什么才能在应用程序打开时将 Google 加载到网络 View 中?

    我做了一些研究并走到了这一步。我能够找到一个工作示例,但我试图逐步了解它是如何工作的,并且卡在了这一步上。

    最佳答案

    如果您希望您的代理人在应用程序启动后加载 Google 主页,您只需将以下内容添加到您的 applicationDidFinishLaunching: :

    NSURL*        url     = [NSURL URLWithString:@"http://google.com"];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    [[self.webView mainFrame] loadRequest:request];

    这假设您的 AppDelegate.h 中有以下内容:
    @property (weak) IBOutlet WebView *webView;

    您通过 CTRL 拖动从 WebView 放置的Interface Builder 中的控件到您的 AppDelegate.h .

    最终代码:
    AppDelegate.h
    #import <Cocoa/Cocoa.h>
    #import <WebKit/WebKit.h>

    @interface AppDelegate : NSObject <NSApplicationDelegate>

    @property (assign) IBOutlet NSWindow *window;
    @property (weak) IBOutlet WebView *webView;

    @end
    AppDelegate.m
    #import "AppDelegate.h"

    @implementation AppDelegate

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    NSURL* url = [NSURL URLWithString:@"http://google.com"];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    [[self.webView mainFrame] loadRequest:request];

    }

    @end

    关于objective-c - Xcode 4.5.2 - OS X 应用程序 - 将 webview 链接到代码,加载手工编码的主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14794091/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com