2

I have tab bar based application (iOS 7.1 SDK). When user start app at first time, I want show some login screen. I decided to use view controller containment (this is called in first view controller of tab bar controller):

LoginViewController *vc = [LoginViewController new];
[self.tabBarController addChildViewController:vc];
[vc didMoveToParentViewController:self.tabBarController];
[self.tabBarController.view addSubview:vc.view];

But there are some problems. View is normally visible, but in LoginViewController viewWillAppear and viewDidAppear are never called. I try to use this piece of code in all view lifecycle methods (viewDidLoad, viewWillAppear, viewDidAppear), but with no luck. I know there are some other ways to achieve what i'm trying to do. For example add child controller to first view controller of tab bar controller and hide tab bar, which works great and viewWillAppear and viewDidAppear are normally called. But because of this I get even more curious - why adding child view controller to tab bar controller don't work as expected?

lukasMT
  • 41
  • 3
  • You need to present or push the viewcontroller in order for the methods to get called. Just adding the view as a subview will not work. – ZeMoon Jul 15 '14 at 09:30

2 Answers2

0

You need to present or push the viewcontroller in order for the methods to get called. Just adding the view as a subview will not work.

In your case, you can explicitly call the viewWillAppear, viewDidAppear methods.

LoginViewController *vc = [LoginViewController new];
[self.tabBarController addChildViewController:vc];
[vc didMoveToParentViewController:self.tabBarController];
[self.tabBarController.view addSubview:vc.view];

[vc viewWillAppear];
[vc viewDidAppear];
ZeMoon
  • 20,054
  • 5
  • 57
  • 98
  • 1
    Thanks for the answer. But I think that call these methods explicitly is not good solution. They should be called automatically when view controller containment is used, right? As I saying, when I used my code in normal view controller which is one of view controllers in tab bar controller, everything works as expected. I have problems only when I try add child view controller directly to tab bar controller. – lukasMT Jul 15 '14 at 09:49
  • Adding a viewController's view directly to another is also not a good implementation. When you use such a system, you will have to use workarounds like this. If you want the methods to be called by themselves, you need to present it properly. – ZeMoon Jul 15 '14 at 10:01
-2

It's working!

dispatch_async(dispatch_get_main_queue(), ^{
        [self.tabBarController setSelectedIndex:0];
        [self.tabBarController setSelectedIndex:1];
        [self.tabBarController setSelectedIndex:0];
    });
SamSol
  • 2,885
  • 6
  • 37
  • 56