web-dev-qa-db-fra.com

Ajouter un contrôleur de barre d'onglets par programme au flux d'application actuel

Je souhaite ajouter un contrôleur de barre d'onglets à mon flux d'application actuel. Actuellement, j'ai une page avec un bouton qui en cliquant ouvre un nouveau contrôleur de vue avec une vue Web où l'utilisateur se connecte et après la connexion, je veux l'emmener sur sa page d'accueil où la barre de navigation a son nom et un bouton de déconnexion à droite . La page d'accueil doit également avoir une barre d'onglets avec 3 onglets différents. Je peux charger la vue de la page d'accueil à partir de la vue Web et obtenir la barre de navigation. Mais je ne peux pas ajouter le tabBar et le faire fonctionner. Je ne sais pas où ajouter le code pour ajouter TabBar. J'utilise le code ci-dessous pour ajouter une barre d'onglets -

UITabBarController *tabBar = [[UITabBarController alloc] init];

HomeViewController *home = [[PPHomeViewController alloc] initWithUserName:[self.userInfo objectForKey:@"name"] Email:[self.userInfo objectForKey:@"email"] Phone:[self.userInfo objectForKey:@"phone_number"]];
home.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
UINavigationController *homeNavController = [[UINavigationController alloc]initWithRootViewController:home];

RequestViewController *req = [[RequestMoneyViewController alloc]init];
req.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];
UINavigationController *reqNavController = [[UINavigationController alloc]initWithRootViewController:req];

UIViewController *thirdViewController = [[UIViewController alloc]init];
thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];
UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController];

UIViewController *fourthViewController = [[UIViewController alloc]init];
thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];
UINavigationController *fourthNavController = [[UINavigationController alloc]initWithRootViewController:fourthViewController];

tabBar.viewControllers = [[NSArray alloc] initWithObjects:homeNavController, reqNavController, thirdNavController, fourthNavController, nil];
tabBar.delegate=self;
tabBar.selectedIndex=0;

UIImageView *homeImg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 432, 80, 49)];
homeImg.tag=11;
homeImg.image=[UIImage imageNamed:@"footer"];

UIImageView *reqImg=[[UIImageView alloc]initWithFrame:CGRectMake(81, 432,80, 49)];
reqImg.tag=12;
reqImg.image=[UIImage imageNamed:@"footer"];

UIImageView *sendImg=[[UIImageView alloc]initWithFrame:CGRectMake(162, 432,80, 49)];
sendImg.tag=13;
sendImg.image=[UIImage imageNamed:@"footer"];

UIImageView *localImg=[[UIImageView alloc]initWithFrame:CGRectMake(243, 432, 80, 49)];
localImg.tag=14;
localImg.image=[UIImage imageNamed:@"footer"];

[tabBar.view addSubview:homeImg];
[tabBar.view addSubview:reqImg];
[tabBar.view addSubview:sendImg];
[tabBar.view addSubview:localImg];

[[[UIApplication sharedApplication]keyWindow]addSubview:tabBar.view];

Actuellement, j'ai mis le code ci-dessus dans le viewDidLoad d'un ViewController TabViewController qui étend UITabBarController. Dans mon contrôleur webView, j'ai mis le code suivant -

TabViewController *tab=[[TabViewController alloc] init];
tab.userInfo=userInfo;
[self presentViewController:tab animated:YES completion:nil];

Mais l'application se bloque dès que je clique sur un onglet autre que celui déjà ouvert. Veuillez aider.

15
Akshay Goel

La façon dont j'ai fait cela dans le passé est de créer une sous-classe UITabBarController qui contient tout le code de création tabBar que vous avez ci-dessus.

Utilisez ensuite votre UINavigationController pour pousser la sous-classe tabBar à l'écran.

Voici un exemple de ma sous-classe UITabBarController:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIViewController *view1 = [[UIViewController alloc] init];
    UIViewController *view2 = [[UIViewController alloc] init];
    UIViewController *view3 = [[UIViewController alloc] init];

    NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
    [tabViewControllers addObject:view1];
    [tabViewControllers addObject:view2];
    [tabViewControllers addObject:view3];

    [self setViewControllers:tabViewControllers];
    //can't set this until after its added to the tab bar
    view1.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view1" 
                                    image:[UIImage imageNamed:@"view1"] 
                                      tag:1];
    view2.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view2" 
                                    image:[UIImage imageNamed:@"view3"] 
                                      tag:2];
    view3.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view3" 
                                    image:[UIImage imageNamed:@"view3"] 
                                      tag:3];      
}
42
andrew lattis

Définir Delegate UITabBarDelegate

ici Image TabBar Viewcontroller http://prntscr.com/ba5oks

#pragma mark- Tapbar delegate

- (void)deselectTabBarItem:(UITabBar*)tabBar
{
    tabBar.selectedItem = nil;
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    [self performSelector:@selector(deselectTabBarItem:) withObject:tabBar afterDelay:0.2];

    switch (item.tag) {
        case 0:
            //perform action
            break;
        case 1:
            //do whatever you want to do.
            break;
        case 2:
            //call method
            break;
        default:
            break;
    }
}
2
Manjeet