Loading a view controller & view hierarchy programmatically in Cocoa Touch without xib

前端 未结 2 1479
孤城傲影
孤城傲影 2020-12-05 05:48

It seems like all of the Cocoa Touch templates are set up to load a nib.

If I want to start a new project that\'s going to use a view controller, and load its view(

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 06:50

    UIViews themselves do not have a hierarchy, UINavigationControllers do. So init one of those, and pop a UIViewController onto it's stack. This should do it, in the most basic way possible, with no XIB files at all. You should be able to build on this.

    - (void)applicationDidFinishLaunching:(UIApplication *)application {    
       UINavigationController *navController = [[UINavigationController alloc] init];
    
       UIViewController *viewController = [[UIViewController alloc] init];
    
       // set the properties of viewController here, to make it look like you want
    
       [navController pushViewController:viewController animated:NO];
    
       [window addSubview:navController.view];
    
       // Don't forget memory management
       [navController release];
       [viewController release];
    
       [window makeKeyAndVisible];
    }
    

提交回复
热议问题