【发布时间】:2013-08-05 00:29:28
【问题描述】:
我在启动应用程序时看不到 web 视图。 在委托中这是我的代码
#import <Cocoa/Cocoa.h>
#include "NewsViewController.h"
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (nonatomic,strong) IBOutlet NSSplitView *splitView;
@property (nonatomic,strong) IBOutlet NewsViewController *newsViewController;
@end
#import "AppDelegate.h"
@implementation AppDelegate
- (void)dealloc
{
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
// 1. Create the master View Controller
self.newsViewController = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:nil];
// 2. Add the view controller to the Window's content view
[self.splitView addSubview:self.newsViewController.view];
}
@end
这是我的视图控制器
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface NewsViewController : NSViewController{
IBOutlet WebView *web;
}
@property (assign) IBOutlet WebView *web;
@end
#import "NewsViewController.h"
@interface NewsViewController ()
@end
@implementation NewsViewController
@synthesize web;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
-(void)loadView{
NSString *urlAddress = @"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[web mainFrame] loadRequest:requestObj];
}
@end
如果我在控制器中的视图上显示一个标签,一切都很好,但如果我放一个 webview,我只会看到一个灰色窗口。
为什么会这样? 谢谢
【问题讨论】:
-
loadView 和 viewDidLoad 根本不是同一种方法,并且不能像您看起来那样互换使用。
-
解决方案是什么?我无法理解...每次加载控制器时都会调用 LoadView...我确定是因为放了一个 nslog 来澄清我的疑问
-
你最终解决了这个问题吗?从头开始创建新项目时遇到了同样的问题
标签: objective-c macos webview