jwknz
jwknz

Reputation: 6814

Setting up a UINavigation Controller on a UIViewController as rootView

I had a tabBarApplication that I have as a template which has a UINavigation template on each tab.

I want to use that sample (sort of) and convert it into a single UIViewController in a nother application. I have presented 2 pieces of code, the first one is my template and the latter is what I am trying to make. Could anyone please give me some hints or help with how to that right? I keep getting errors, but they do not make sense to me as the tabBarApp does not need it a viewController declared.

First Code (the example):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setupFetchedResultsController];

if (![[self.fetchedResultsController fetchedObjects] count] > 0 ) {
    NSLog(@"!!!!! ~~> There's nothing in the database so defaults will be inserted");
    [self importCoreDataDefaultRoles];
}
else {
    NSLog(@"There's stuff in the database so skipping the import of default data");
}

// The Tab Bar
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

// The Two Navigation Controllers attached to the Tab Bar (At Tab Bar Indexes 0 and 1)
UINavigationController *personsTVCnav = [[tabBarController viewControllers] objectAtIndex:0];
UINavigationController *rolesTVCnav = [[tabBarController viewControllers] objectAtIndex:1];
return YES;
}

Second Code (what I am trying to make):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setupFetchedResultsController];

if (![[self.fetchedResultsController fetchedObjects] count] > 0 ) {
    NSLog(@"!!!!! ~~> There's nothing in the database so defaults will be inserted");
    [self importCoreDataDefaultRoles];
}
else {

UIViewController *mainViewController = (UIViewController *)self.window.rootViewController;

UINavigationController *readingsTVCnav = [[mainViewController viewController] objectAtIndex:0];

// Override point for customization after application launch.
return YES;
}

The fetching parts of the code are related to Core Data which already set up and working. The reason for this change is that I want to have a plain view controller set up as the initial screen rather than the tabBarConfiguration.

Cheers Jeff

EDIT: I have added an image for clarity

enter image description here

Upvotes: 0

Views: 327

Answers (1)

jonkroll
jonkroll

Reputation: 15722

From what you describe and depict in your image, you have the navigation controller set up as your app's root view controller. So you can access it (if you need to) in your app delegate's didFinishLaunchingWithOptions: method as follows:

UINavigationController *navController = (UINavigationController *)self.window.rootViewController;

Although for your app I don't think there's any reason you need to reference the nav controller from your app delegate. All the setup you need (with the exception of your code data code, which you say is already working) can be handled through the storyboard.

In your nagivationController's root viewController (the one with all the buttons) you should set up a segue from each button to the approperiate viewController in your storyboard. Bure sure to set then up as "push" segues so that it will push the view controller onto your navigationController's navigation stack. If there is any particular setup you need to do for the child view controller when the segue happens, you can implement the prepareForSegue: method like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ShowMyViewController"]) {

        MyViewController *vc = (MyViewController*)segue.destinationViewController;       
        vc.title = @"My Title";
        vc.someProperty = @"Some Value";
    }
}

You can (and should) identify each of your segues in the storyboard with a unique identifier so that you can identify them when this method is called.

Upvotes: 1

Related Questions