2013-05-06 8 views
15

Chcę dodać pasek kontrolera paska do mojego bieżącego przepływu aplikacji. Aktualnie mam stronę z przyciskiem, który po kliknięciu otwiera nowy viewcontroller z widokiem WWW, w którym loguje się użytkownik, a po zalogowaniu chcę zabrać go na swoją stronę domową, gdzie pasek nawigacyjny ma swoje imię i przycisk wylogowania po prawej stronie . Strona główna powinna również mieć pasek zakładek z 3 różnymi zakładkami. Jestem w stanie załadować widok strony głównej ze strony internetowej i uzyskać pasek nawigacji. Ale nie jestem w stanie dodać tabBar i uruchomić go. Nie wiem, gdzie dodać kod do dodawania TabBar. Używam poniższy kod, aby dodać pasek kart -Dodaj kontroler paska kart programowo do bieżącego przepływu aplikacji

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

Obecnie mam umieścić powyższy kod w viewDidLoad z ViewController TabViewController która rozciąga UITabBarController. W moim kontrolerze webView umieściłem następujący kod -

Ale aplikacja ulega awarii, gdy tylko kliknę inną kartę niż ta, która już jest otwarta. Proszę o pomoc.

+0

trzeba sprawdzić widoku drzewa ... W której widok dodajesz pasek kart .. – Shivaay

+0

Chcę dodać tabBar w widoku, który przychodzi po zalogowaniu w - to jest widok po widoku internetowym w drzewie widoku. –

+0

Czy próbowałeś utworzyć 'UINavigationController' jako root viewcontroller i przesuwając swój TabViewController po zalogowaniu? – Devang

Odpowiedz

39

Sposób, w jaki to zrobiłem w przeszłości, polega na utworzeniu podklasy UITabBarController zawierającej cały kod kreacji, który posiadasz powyżej.

Następnie użyj swojego UINavigationController, aby przesunąć podklasę tabBar na ekran.

Oto próbka mojego UITabBarController podklasy:

- (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

Dobra odpowiedź działa świetnie –

2

Set Delegat UITabBarDelegate

tutaj umieszczenie zakładek Viewcontroller obraz 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; 
    } 
}