如何在Cocoa中将视图控制器设置为根视图?

时间:2013-09-02 06:03:24

标签: objective-c macos cocoa nsview

我有AppDelegatemainWindow.xib。我创建了另一个viewController并从AppDelegate调用,它运行良好。现在我怀疑是否可以将视图控制器设置为root,而不将其添加到mainWindow.xib。是否必须使用mainWindow.xib加载我们的观点?

我正在调用这样的视图控制器

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    self.view = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    [self.window.contentView addSubview:self.view.view];
    self.view.view.frame = ((NSView*)self.window.contentView).bounds;
}

3 个答案:

答案 0 :(得分:3)

尝试使用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    self.viewControllerObj = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = self.viewControllerObj;

    [self.window makeKeyAndVisible];
    return YES;

}

答案 1 :(得分:1)

试试这个:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{   
    self.view = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = self.view;   
}

答案 2 :(得分:0)

1.将此内容添加到您的appdelegate.h

@property (strong, nonatomic) UIWindow *window;

2.在didFinishLaunching方法中将此添加到appdelegate.m

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

return YES;