2013-05-06 19 views
15

Tôi muốn thêm một Thanh Điều khiển Thanh vào Luồng ứng dụng hiện tại của mình. Hiện tại tôi có một trang có nút bấm khi mở một khung nhìn mới với chế độ xem web, nơi người dùng đăng nhập và sau khi đăng nhập, tôi muốn đưa anh ta đến trang chủ của anh ấy, nơi thanh điều hướng có tên của anh ấy và nút đăng xuất ở bên phải . Trang chủ cũng phải có một thanh tab với 3 tab khác nhau. Tôi có thể tải chế độ xem trang chủ từ chế độ xem web và nhận thanh điều hướng. Nhưng tôi không thể thêm tabBar và làm cho nó hoạt động. Tôi nhầm lẫn về nơi để thêm mã để thêm TabBar. Tôi đang sử dụng mã dưới đây để thêm thanh tab -Thêm Bộ điều khiển Thanh tab Lập trình cho Dòng Ứng dụng hiện tại

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

Hiện nay tôi đã đặt mã trên trong viewDidLoad của một TabViewController ViewController kéo dài UITabBarController. Trong bộ điều khiển webView của tôi, tôi đã đặt mã sau đây -

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

Nhưng ứng dụng gặp sự cố ngay khi tôi nhấp vào bất kỳ tab nào khác ngoài tab đang mở. Vui lòng trợ giúp.

+0

bạn phải kiểm tra cây tầm nhìn của bạn ... Trong đó xem bạn đang thêm thanh tab .. – Shivaay

+0

Tôi muốn thêm tabBar trong chế độ xem xuất hiện sau khi đăng nhập - tức là chế độ xem sau chế độ xem web trong cây chế độ xem. –

+0

Bạn đã thử tạo một 'UINavigationController' như một trình điều khiển xem gốc và đẩy TabViewController của bạn khi đăng nhập? – Devang

Trả lời

39

Cách tôi đã thực hiện việc này trong quá khứ là tạo một lớp con UITabBarController chứa tất cả mã tạo mã tabBar mà bạn có ở trên.

Sau đó, sử dụng UINavigationController để đẩy lớp con tabBar vào màn hình.

Dưới đây là một mẫu của UITabBarController lớp con của tôi:

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

Câu trả lời hay khi làm việc tuyệt vời –

2

Set Đại biểu UITabBarDelegate

đây TabBar ViewController ảnh 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; 
    } 
}