OS X如何指定启动视图控制器

时间:2015-07-16 12:52:32

标签: objective-c macos

我正在mac上开发一个简单的浏览器项目,我没有使用默认的viewcontroller。

相反,我编写了一个viewcontroller类BrowserViewController。 并在appdelegate中写下以下内容

@interface AppDelegate()
@property (nonatomic, strong) BrowserViewController *browserController;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
  // Insert code here to initialize your application
    self.browserController = [[BrowserViewController alloc] init];

    [self.window makeKeyWindow];
    [self.window setContentView:self.browserController.view];
}

@end

但是当应用程序启动时,它会导致默认的viewcontroller而不是BrowserViewController。我真的不知道原因。

感谢您的帮助,我已经解决了这个问题。我的解决方案是这样的:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
//set the frame of the window fit to the device's frame
//
self.window = [[NSWindow alloc] initWithContentRect:[[NSScreen     mainScreen] frame] styleMask:NSBorderlessWindowMask     backing:NSBackingStoreBuffered defer:NO ];

//
//
self.browserController = [[BrowserViewController alloc] init];


//set contentView
//
self.window.contentViewController = self.browserController;

//this is setting global backgroundColor of the window
//
self.window.backgroundColor = [NSColor whiteColor];

//this means the window is the window that will receive user interaction.
//
[self.window makeKeyAndOrderFront:self];
//[self.window makeKeyWindow];//NOTE: This is not working.
}

3 个答案:

答案 0 :(得分:2)

您可以在故事板中选择Is Initial Controller

storyboard

答案 1 :(得分:0)

听起来像你正在寻找(在你的didFinishLaunchingWithOptions方法中):

self.browserController = [[BrowserViewController alloc] init];
[self.window setRootViewController: self.browserController];
[self.window makeKeyAndVisible];

答案 2 :(得分:0)

如果您想以编程方式进行此操作:

注意

这适用于iOS,但逻辑相同。希望这会因某些原因而有用.. :)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //set the frame of the window fit to the device's frame
    //
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    //what you have above is correct
    //
    self.browserController = [[BrowserViewController alloc] init];

    //you can also directly set it like
    //
    BrowserViewController *mainView = [[BrowserViewController alloc] init];

    //this is the most important, you need to set the window's rootViewController
    //
    self.window.rootViewController = mainView;

    //this is setting global backgroundColor of the window
    //
    self.window.backgroundColor = [UIColor whiteColor];

    //this means the window is the window that will receive user interaction.
    //
    [self.window makeKeyAndVisible];

    return YES;
}

希望这是有用的和有益的..干杯! :)