2013-05-06 2 views
15

私の現在のApp FlowにTab Bar Controllerを追加したいと思います。現在のところ、クリックすると、ユーザーがログインした後、ログインした後で、ナビゲーションバーの名前とログアウトボタンが右側に表示されているホームページに移動したいというWebビューの新しいviewcontrollerを開くボタンがあるページがあります。ホームページには3つのタブがあるタブバーも必要です。 webviewからホームページビューを読み込み、ナビゲーションバーを取得できます。しかし、私はtabBarを追加することはできませんし、それを動作させる。 TabBarを追加するコードをどこに追加するのか混乱しています。私は、タブバーを追加するには、以下のコードを使用しています -Tab Bar Controllerを現在のApp Flowにプログラムで追加する

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]; 

現在、私はUITabBarControllerを拡張するのViewController TabViewControllerののviewDidLoadで上記のコードを入れています。私のWebViewコントローラでは、私は次のコードを入れている -

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

をしかし、私はすでに開いて1以外の任意のタブをクリックするとアプリが、すぐにクラッシュします。 助けてください。

+0

あなたがタブバーを追加しているのを見てください.. – Shivaay

+0

私はログ後に来るビューにtabBarを追加したいビューツリー内のウェブビューの後のビュー。 –

+0

'UINavigationController'をルートviewcontrollerとして作成し、ログイン時にTabViewControllerを押してみましたか? – Devang

答えて

39

私が過去に行ったことは、上記のtabBar作成コードのすべてを含むUITabBarControllerサブクラスを作成することです。

次に、UINavigationControllerを使用して、tabBarサブクラスを画面に押します。

ここに私の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];  
} 
+0

ニースの回答がうまくいっています –

2

セットの委任UITabBarDelegate

の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; 
    } 
} 
関連する問題