2011-12-22 76 views
0

大家好,我需要一些幫助。我正在嘗試爲Xcode 4.0中的mac創建一個應用程序,並且我希望它在啓動時顯示一個網頁,我已經添加了webkit框架並獲得了一些代碼,但它不工作。代碼如下如何在打開應用程序時啓動網頁

的應用delegate.m文件

#import "AppDelegate.h" 

#import <WebKit/WebKit.h> 

@implementation AppDelegate 

@synthesize window = _window; 


@synthesize WebView; 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    NSString *urlAddress = @"http://www.google.com/"; 
    NSURL *url = [NSURL URLWithString:urlAddress]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [[WebView mainFrame] LoadRequest:requestObj]; 
} 

@end 

和應用程序delegate.h文件

#import <Cocoa/Cocoa.h> 

#import <WebKit/WebKit.h> 

@interface AppDelegate : NSObject <NSApplicationDelegate> 

@property (assign) IBOutlet NSWindow *window; 

@property (assign) IBOutlet WebView *WebView; 

@interface myAppDelegate : NSObject <NSApplicationDelegate> { 
    WebView *myWebView; 

} 

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

@end 

如果有人可以幫助請儘快迴應謝謝你們!

回答

0

嗯,我假設代碼不編譯? (它不應該!:))。在.H應該是這個樣子:

#import <Cocoa/Cocoa.h> 

#import <WebKit/WebKit.h> 

@interface AppDelegate : NSObject <NSApplicationDelegate> { 
    WebView *_myWebView; // not needed if you're using the latest xcode 
} 

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

@end 

與.m:

#import "AppDelegate.h" 

#import <WebKit/WebKit.h> 

@implementation AppDelegate 

@synthesize window = _window; 
@synthesize myWebView = _myWebView; 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    NSString *urlAddress = @"http://www.google.com/"; 
    NSURL *url = [NSURL URLWithString:urlAddress]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [[self.myWebView mainFrame] loadRequest:requestObj]; // note the lower case 'l' in 'loadRequest' 
} 

@end 

這是假定您已經連接的WebView與myWebView屬性的.xib。

+0

謝謝解決它的另一個原因是我忘了將web視圖連接到mywebview屬性 – Blizardy

相關問題