Kal Calender Disappears when clicked Tab

66 Views Asked by At

When I click the tab bar that holds my kal calendar the calendar disappears:

enter image description here

This is my code in viewWillAppear:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    KalViewController *calendar = [[KalViewController alloc] init];
    [self.navigationController pushViewController:calendar animated:YES];
    calendar.dataSource = self;
    calendar.delegate = self;
    [calendar reloadData];
    self.tabBarController.delegate = self;
}

I also have this method:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
     [self viewWillAppear:YES];
     //Even when I comment out this line the problem stays!
}

If you need more infomation just ask.

EDIT:

enter image description here

1

There are 1 best solutions below

12
sergio On BEST ANSWER

You are using your tab bar controller improperly. Indeed, you should populate it with your controllers (either in Interface Builder or by setting the tab bar controller's controllers property) and let it do its job.

On another coin, note that viewWillAppear can be called multiple times, so this is the last place where you want to allocate a new controller. If you have overridden viewWillAppear in order for your controller to show something when it s view is displayed that is not the correct approach.

EDIT:

If you create your UI programmatically, these are the steps you should follow in you application:didFinishLaunching: to setup the rootViewController:

FirstViewController *fistVC = ...
SecondViewController *secondVC = ...
ThirdViewController *thirdVC = ...

NSArray *viewControllers = [[NSArray alloc] initWithObjects:fistVC, secondVC, thirdVC, nil];

self.tabController = [[UITabBarController alloc] init];
[self.tabController setViewControllers:viewControllers animated:YES];

self.window.rootViewController = self.tabController;

If you build your UI in Interface Builder, you will do the same graphically, but I cannot reproduce it here.

In any case, I hope this snippet clarifies the way the tab bar controller works: you instantiate all of its controllers, put them in an array, pass the array to the tab bar controller. No need to instantiate anymore the tabbed controllers (neither in viewWillAppear nor in viewDidLoad)...