Error creating Tabbarcontroller at runtime

Asked

Viewed 43 times

0

I’m having a problem creating a Tabbarcontroller at runtime in objective-c, when I call the method that creates the tabbar an error occurs:

Thread 11: EXC_BAD_ACCESS(code=1, addess=0x3000000c)

I believe it’s because I created a Navigationcontroller as Rootviewcontroller no Appdelegate, but I can not use the tabbar as Rootviewcontroller, because I will only use this tabbar in a given view, someone has an idea of how to do this without this type of error, follow the code.

-(void)criarTab{

MenuOpcao *vc1 = [[MenuOpcao alloc] initWithNibName:@"MenuOpcao" bundle:[NSBundle mainBundle]];
Carrinho *vc2 = [[Carrinho alloc] initWithNibName:@"Carrinho" bundle:[NSBundle mainBundle]];

NSMutableArray *topLevelControllers = [[NSMutableArray alloc] init];

[topLevelControllers addObject: vc1];
[topLevelControllers addObject: vc2];

vc1.title = @"Carrinho";
vc2.title = @"Tipos de Pratos";

vc1.tabBarItem.image = [UIImage imageNamed:@"filtro@2x"];
vc2.tabBarItem.image = [UIImage imageNamed:@"carrinho@2x"];

NSString *valorBagde =[[NSString alloc] initWithFormat:@"%lu", (long)ContaCarrinho ];

vc2.tabBarItem.badgeValue=valorBagde;

tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;

@try {
   [tabBarController setViewControllers:topLevelControllers animated:YES];


    tabBarController.selectedIndex = 0;
    tabBarController.view.bounds = [[UIScreen mainScreen] bounds];
    tabBarController.view.frame = [[UIScreen mainScreen] bounds];

    tabBarController.view.frame = CGRectMake(0,0,self.view.frame.size.width,h);
    [self.view addSubview:tabBarController.view];

    Tabok=2;

    [self.tabBarController.tabBar setBackgroundColor:[UIColor colorWithHexString:@"#ed2a69"]];




}
@catch (NSException *exception) {
    NSLog(@"%@", exception.reason);
    NSLog(@"%@", exception.description);
}
@finally {


}

}

1 answer

1

It is not recommended to push a UITabBarController in your app from a UINavigationViewController but yes, you can technically do it.

The simplest way to do what you want is this:

// Construa seu tab bar normalmente
UITabBarController *tabBar = [UITabBarController new];
tabBar.delegate = self; // será que você precisa mesmo de um delegate neste caso?

// Adicione todos view controllers que você deseja renderizar nele.
[tabBar setViewControllers:viewControllers animated:NO];

// Se estiver usando size classes, você deve coloca-lo na tela assim
[viewController showViewController:tabBar];

// Senão
[viewController presentViewController:tabBar animated:YES];

This section below is totally unnecessary

tabBarController.selectedIndex = 0;
tabBarController.view.bounds = [[UIScreen mainScreen] bounds];
tabBarController.view.frame = [[UIScreen mainScreen] bounds];

tabBarController.view.frame = CGRectMake(0,0,self.view.frame.size.width,h);
[self.view addSubview:tabBarController.view];

And finally, avoid using as much as possible @try, in addition to slowing down execution, it in this case does not solve the problem if you are just using the @try for debugging, you’d better give preference to console logs or the Xcode Bugger.

  • Thanks for the tip, this problem I already managed to solve, now I have another problem, now when pressing an item of tabbar the navigationbar is not displayed you would know how I can solve this?

  • For every item of your UITabView, you have to add one UINavigationView. If in this case, you want the navigation bar to be shared between all tabs, then you will have to customize a Uitabview from a Uiviewcontroller, Apple does not recommend that a Uinavigationcontroller push an Uitabbarcontroller inside itself.

  • 1

    It was clear, but in case I use a tabbar content two controls and also use a navigationbar, navigation only has buttons but I use the tab to navigate between the screens.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.