UILabel added to UITabbarController view in xib got Hidden by UITabbarController child view controllers

StackOverflow https://stackoverflow.com/questions/23654661

  •  22-07-2023
  •  | 
  •  

Question

UITabbarController.xib has UILabel as SubView.

When i run the app UILabel is not visible as the child view controllers are hiding UILabel.

I wanted that UILabel to be visible in all the ChildView controllers.

When I debug the code in viewDidLoad and viewWillAppear that label looks nil object though i have IBOutlet connection in the TabBarController.h file.

I have Code like this

in .h file

@interface TestTabBarController : UITabBarController <UITabBarControllerDelegate>

@end
and in .m file

@interface TestTabBarController ()
@property (weak, nonatomic)IBOutlet UILabel *historylabel;
@end


@implementation TestTabBarController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]){

    }
    return self;
}
-(void)setChildrenViewControllers
{
    // Set the View Controllers.
    FirstViewController *viewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController"  bundle:nil];

    SecondViewController *viewcontroller2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    ThirdViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];

    FourthViewController *viewController4 = [[FourthViewController alloc] initWithNibName:@"FourthViewController"   bundle:nil];

self.viewControllers = @[viewController,viewcontroller2,viewController3,viewController4];


self.delegate = self;
}

@end

I am calling the Tabbarcontroller class as follows

TestTabBarController *testtabbar = [[TestTabBarController alloc] initWithNibName:@"TestTabBarController" bundle:nil];
[testtabbar setChildrenViewControllers];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:testtabbar];
[self.navigationController presentViewController:navController animated:YES completion:nil];
Was it helpful?

Solution

I have Xib for the UITabbarController subclass - which is a view.If i add the label on top of view in xib it will not display if because viewcontroller is subclass of tabbarcontroller.tabbarcontroller accepts only tabbar as subview and it will not accept labels.So We have to add items to each child controller or else in viewdidload of tabbarcontroller class create a label alloc and init it and add it like

[self.view addsubview:label];

if you add label on xib view then it will not be visible on child view controllers,if you load nib forcefully in viewdidload then nib will be visiblw but not tabbarcontroller and child view controllers.Hope this will help everyone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top