2012-04-19 22 views
5

Tôi đã xây dựng ứng dụng của mình với bảng phân cảnh và tất cả các chế độ xem được quản lý bởi một thanh điều khiển tabbar.Vô hiệu/Bật các tab trong UITabBarController trong ứng dụng dựa trên kịch bản

Vì vậy, trên mắt (tôi chỉ làm việc trên iPad UI hiện tại) nó thực hiện điều này:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) 
    { 
     UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; 
     UISplitViewController *splitViewController = [tabBarController.viewControllers objectAtIndex:0]; 
     UINavigationController *navigationController = [splitViewController.viewControllers lastObject]; 
     splitViewController.delegate = (id)navigationController.topViewController; 

     UINavigationController *masterNavigationController = [splitViewController.viewControllers objectAtIndex:0]; 
     ProductionMasterViewController *controller = (ProductionMasterViewController *)masterNavigationController.topViewController; 
     controller.managedObjectContext = self.managedObjectContext; 
    } 
} 

Tôi muốn để có thể kích hoạt hoặc vô hiệu hóa các tab trong tabBarController dựa trên người dùng nhập vào (vì vậy ví dụ: một mục cần phải được chọn trong tab đầu tiên để truy cập tab thứ hai và thứ ba, được tắt theo mặc định)

Điều tôi không rõ là cách truy cập các tab để cho phép/giải quyết chúng. Tôi sẽ tạo ra một thể hiện của các appdelegate và sau đó làm một cái gì đó giống như

AppDelegate *d = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
UITabBarController *tabs = (UITabBarController *)[d.window rootViewController]; 
[[[[tabs tabBar] items] objectAtIndex:2] setEnabled:YES]; 
[[[[tabs tabBar] items] objectAtIndex:3] setEnabled:YES]; 
[[[[tabs tabBar] items] objectAtIndex:4] setEnabled:YES]; 

(Đó là kinda có vẻ như nó sẽ làm việc nhưng nó cũng có vẻ khá thô thiển.)

Trả lời

4

Vì bạn đang sử dụng một ứng dụng Storyboard dựa, Tôi cho rằng bạn có số UITabBarController được định nghĩa trong bảng phân cảnh làm bộ điều khiển gốc. Ngẫu nhiên, bạn cũng có thể truy xuất nó bằng mã định danh thay vì đi bộ từ cửa sổ đến trình điều khiển chế độ xem gốc.

Hạn chế tab nào có thể chọn được, đạt được bằng cách đặt đại biểu của UITabBarController (tức là một đại biểu phù hợp với UITabBarControllerDelegate).

Trong các đại biểu, bạn có thể thực hiện hai phương pháp:

– tabBarController:shouldSelectViewController:

– tabBarController:didSelectViewController:

có khả năng, bạn chỉ cần là người đầu tiên để hạn chế (ức chế) lựa chọn, cho đến khi công việc của bạn đã sẵn sàng.

Cách tiếp cận khác, là đặt thuộc tính "viewControllers" trên thanh điều khiển thanh tab, mỗi lần một cột mốc được thông qua. Mỗi mốc, bạn đặt một loạt các bộ điều khiển chế độ xem mở rộng vào thuộc tính này, sẽ mở ra lựa chọn các mục tab bổ sung.

SWIFT 3

(mở rộng cho dễ hiểu)

let arrayOfTabBarItems = tabBarController?.tabBar.items 

     if let barItems = arrayOfTabBarItems, barItems.count > 0 { 
      os_log("barItems.count is now ", barItems.count) 
      tabBarItem0 = barItems[0] 
      tabBarItem0.isEnabled = true 
      tabBarItem1 = barItems[1] 
      tabBarItem1.isEnabled = true 
      tabBarItem2 = barItems[2] 
      tabBarItem2.isEnabled = true 
      tabBarItem3 = barItems[3] 
      tabBarItem3.isEnabled = true 
      tabBarItem4 = barItems[4] 
      tabBarItem4.isEnabled = true 
     } 

này có thể được sử dụng trong viewWillAppear của bạn trên ea điều khiển tab ch. Kiểm tra các quy tắc của bạn chống lại điều này và hạn chế từng tab cho phù hợp. (phương pháp ngắn gọn hơn)

let arrayOfAllTabBarItems = tabBarController?.viewControllers 
    if let tabBarArray = arrayOfAllTabBarItems, tabBarArray.count > 0 { 
     for x in 0...tabBarArray.count-1 { 
      let tabBarItem = tabBarArray[x] 
      if tabBarItem.title != nil { 
       if tabBarItem.title == "Tab1" || tabBarItem.title == "MyTab" || tabBarItem.title == "Tab2Check" { 
        tabBarItem.tabBarItem.isEnabled = !(isMyRuleTrue!) 
       } 
      } 
     } 
    } 
Các vấn đề liên quan