How to install view controller as root view in Cocoa?

I have AppDelegate and mainWindow.xib . I created another viewController and called from AppDelegate , and it works well. Now I doubt whether it is possible to make a view controller with root privileges without adding it to mainWindow.xib . Should it load our view with mainWindow.xib ?

I call the view controller as follows

 - (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; } 
+4
source share
3 answers

Try the following:

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

Try using

 - (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; } 
+4
source

1.add this on your appdelegate.h

 @property (strong, nonatomic) UIWindow *window; 

2. attach this to your appdelegate.m in the didFinishLaunching method

 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; 
+1
source

Source: https://habr.com/ru/post/1500079/


All Articles