0
votes

I am having an issue with trying to load a viewcontroller onto another viewcontroller as a subview.

what I have is a NavigationController that loads some viewControllers in as views (pop and push etc) that works perfectly. then I have decided to put a tabBar into a viewController which then looks after all of the selection stuff using a switch statement, this switch statement then calls a method inside one of the viewControllers that appears inside the navigationController. The method inside this viewController then trys to set another viewcontroller as a subview to the viewcontroller thats inside the navgiation controller.

this is my code.

TabBarViewController.m

#import "DetailViewController.h"

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    switch (item.tag) {
        case 0:
        {
             NSLog(@"item 1 selected");
            DetailViewController *dVC = [[DetailViewController alloc] init];
            [dVC tabBarSelectedAction];
        }
            break;
        default:
            break;
    }

}

so this catches the selected item on the tab bar... then fires off a msg to the DetailViewController method to load the new subview onto DetailViewController.view

- (void)tabBarSelectedAction
{
    ButtonOneViewController *b1VC = [[ButtonOneViewController alloc] initWithNibName:@"ButtonOneViewController" bundle:[NSBundle mainBundle]];
    [self.testView addSubview:b1VC.view];

}

and this is where I am trying to load the subview onto the screen.. I think I am doing it right but for some reason its not displaying.. another thing I would like to do is animate this view from the bottom of the screen up..

any help would be hugely appreciated.

2

2 Answers

0
votes

When you created your new DetailViewController you didn't make it part of the view hierarchy through a push or present type of method. Adding a subview may or may not be working but you won't see it because the object you're adding it to isn't using the screen.

0
votes

Your method should probably look like this. Assuming self DetailViewController.

- (void)tabBarSelectedAction {

   ButtonOneViewController *b1VC = [[ButtonOneViewController alloc] initWithNibName:@"ButtonOneViewController" bundle:[NSBundle mainBundle]];
   [self presentModalViewController:b1VC animated:YES];

}

Even with that, I think your logic is a little screwed up. You allocate and initialize DetailViewController but you never present it anywhere. So how are you expecting to see a modal view in DetailViewController, if you never present it.

EDIT: Taking into consideration your comment of adding it to the UINavigationController, you would change it to look something like this..

  [[self navigationController] presentModalViewController:b1VC animated:YES];

EDIT2: Also, you're initializing a class, just to call a method which is already self. Your -didSelectItem: method should look more like this.

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
switch (item.tag) {
    case 0:
    {
         NSLog(@"item 1 selected");
        [self tabBarSelectedAction];
    }
        break;
    default:
        break;
    }

}