Search code examples
iosobjective-cxcode5

Set up View Controllers in App Delegate Xcode 5


I wanted to know how to set up the app delegate in Xcode 5 since it's different than it was in previous versions. I want the generic view controller files (ViewController.h and .m) to be the files that control the rootViewController I set in my app delegate. Does this happen automatically or do you need to do something in the code? This is how I set up my appDelegate.m

    *(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary   *)launchOptions
{
    CGRect viewRect = [[UIScreen mainScreen]bounds];
    self.window = [[UIWindow alloc]initWithFrame:viewRect];
    UIViewController  *viewController = [[UIViewController alloc]init];
    self.window.rootViewController = viewController;
    [self.window makeKeyAndVisible];

If I wanted my rootView Controller to be a table view controller or something else, would I need to embed it in a basic VC first?


Solution

  • To do it programmatically you can set it to be a UITableViewController since it is a subclass of UIViewController. If you want to use the already create ViewController just change the subclass in the .h file from UIViewController to UITableViewController and add the tableview delegates and datasources into the .m.

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    
    UIViewController *mainViewController = [storyboard instantiateInitialViewControllerWithIdentifier:@"myViewController"];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = mainViewController;
    [self.window makeKeyAndVisible];
    

    or if you want to just draw the view in the view controller create a class and do this

    MyViewController  *viewController = [[MyViewController alloc]init];
    self.window.rootViewController = viewController;
    

    If you are wanting to set your root view via storyboards you can just check the __Is Initial view controller` option

    enter image description here