【问题标题】:Mac OS X using WebView objectMac OS X 使用 WebView 对象
【发布时间】: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


【解决方案1】:

我相信您需要在您的 loadView 实现中调用 [super loadView]

【讨论】:

  • -1 使用 NSViewController 和 UIViewController 调用 super 是不必要的。他什么也没看到,因为他基本上没有分配给view,从而破坏了视图加载机制。
  • -[UIViewcController loadView]'s documentation 明确表示该方法用于以编程方式创建视图,并且不应执行 super 调用。
  • 然而,documentation-[NSViewController loadView] 更简洁,没有这样的说法。事实上,显然是Apple once recommended using load view as a stand in for view did load to those coming from iOS
  • 我有一位同事正在这样做,因为当涉及 NSTableViewCells 时,可以多次调用 awakeFromNib。有一个SO question from someone else on the same issue。我个人从未使用过loadView,无论是在它的iOS 还是Mac 版本中,但Mac 的使用对我来说很清楚。这种方法的底线是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多