【问题标题】:How to set view controller as root view in Cocoa?如何在 Cocoa 中将视图控制器设置为根视图?
【发布时间】:2013-09-02 06:03:23
【问题描述】:

我有一个 AppDelegatemainWindow.xib。我创建了另一个viewController 并从AppDelegate 调用,它运行良好。现在我怀疑是否可以将视图控制器设置为根而不将其添加到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;
}

【问题讨论】:

  • 为什么所有的答案都是关于 UIKit 的

标签: objective-c macos cocoa nsview


【解决方案1】:

尝试使用

- (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;

}

【讨论】:

    【解决方案2】:

    试试这个:

    - (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;   
    }
    

    【讨论】:

    • 通过@property (assign) IBOutlet NSWindow *window;@property (strong, nonatomic) UIWindow *window;声明
    • 该操作正在询问一个可可(桌面 osx)问题。您的UIWindowUIScreen 课程在哪里?
    【解决方案3】:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多