Error creating TabbarController at runtime

0

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

  

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

I think it's because I created a NavigationController as RootViewController in the AppDelegate, but I can not use tabbar as RootViewController , because I will only use this tabbar in a certain view, someone has an idea 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 {


}

}

    
asked by anonymous 15.04.2015 / 15:24

1 answer

1

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

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 MAXIMUM of using @try , in addition to slower execution, in this case, it does not solve the problem, if you're only using @try for debugging, you'd better give preference to console logs or the Xcode debugger.

    
21.04.2015 / 23:25