2012-05-01 22 views
5

Tôi đã triển khai giải pháp UITabBar tùy chỉnh cho một dự án. Về cơ bản, nếu có nhiều hơn 5 mục, tôi sử dụng một scrollView cho phép người dùng cuộn qua các mục tab bổ sung và chặn nút nhiều hơn. Một giao diện tương tự có thể được nhìn thấy trong ứng dụng Weather Channel.Suppress moreNavigationController trong tùy chỉnh UITabBarController

Mỗi mục trên thanh tab tương ứng với UINavigationController quản lý chồng lượt xem cho mỗi tab. Vấn đề tôi gặp phải là khi tôi có nhiều hơn 5 mục tab, từ tab 5 trở đi không duy trì chính xác ngăn xếp điều hướng. Dường như các moreNavigationController giết chết ngăn xếp chuyển hướng mỗi khi bạn quay trở lại tab đó và bạn sẽ được đưa đến trang ban đầu một lần nữa.

Tôi đã ghi đè phương pháp setSelectedViewController như sau:

- (void) setSelectedViewController:(UIViewController *)selectedViewController { 
    [super setSelectedViewController:selectedViewController]; 
    if ([self.moreNavigationController.viewControllers count] > 1) { 
     self.moreNavigationController.viewControllers = [[NSArray alloc] initWithObjects:self.moreNavigationController.visibleViewController, nil]; 
    } 
} 

Mã này sẽ loại bỏ các chức năng Thông tin thêm về nút nav trái nhưng nó không giải quyết được vấn đề của việc duy trì điều hướng stack. Tất cả các tab khác hoạt động tốt. Tôi có thể duyệt qua một số chế độ xem và ngăn xếp được duy trì sau khi tôi rời khỏi và quay lại tab đó. Tôi hiểu rằng đây là một vấn đề phức tạp, vì vậy hãy cho tôi biết nếu có những lĩnh vực mà tôi có thể cung cấp sự rõ ràng. Cảm ơn!

Trả lời

5

Đây là cách tôi đã kết thúc sửa chữa này:

- (void) setSelectedViewController:(UIViewController *) selectedViewController { 
    self.viewControllers = [NSArray arrayWithObject:selectedViewController]; 
    [super setSelectedViewController:selectedViewController]; 
} 

Về cơ bản bất kỳ tab từ 5 trên được điều khiển chuyển hướng của nó thay thế bằng moreNavigationController khi bạn intiially thiết lập viewControllers trên UITabBarController. Do đó, tôi tự động đặt viewControllers để chỉ chứa tab tôi đang nhấp. Không bao giờ kết thúc được nhiều hơn 1 trong trường hợp này để các moreNavigationController không đi vào chơi.

Khi tôi khởi động trình điều khiển tùy chỉnh của mình, tôi chỉ cung cấp tab đầu tiên làm khung nhìn ViewControllers để ứng dụng có thể tải.

- (id) init { 
    self = [super init]; 
    if (self) { 
     self.delegate = self; 
     [self populateTabs]; 
    } 
    return self; 
} 

- (void) populateTabs { 
    NSArray *viewControllers = [self.manager createViewsForApplication]; 
    self.viewControllers = [NSArray arrayWithObject:[viewControllers objectAtIndex:0]]; 
    self.tabBar.hidden = YES; 
    MyScrollingTabBar *tabBar = [[MyScrollingTabBar alloc] initWithViews:viewControllers]; 
    tabBar.delegate = self; 
    [self.view addSubview:tabBar]; 
} 

Để rõ ràng, đại biểu tabBar được đặt thành lớp này để có thể phản hồi các lần nhấp tab. Phương pháp đại biểu như sau:

- (void) tabBar:(id) bar clickedTab:(MyScrollingTabBarItem *) tab { 
     if (self.selectedViewController == tab.associatedViewController) { 
     [(UINavigationController *) tab.associatedViewController popToRootViewControllerAnimated:YES]; 
    } else { 
     self.selectedViewController = tab.associatedViewController; 
    } 
    // keep nav label consistent for tab 
    self.navigationController.title = tab.label.text; 
} 
+1

Tôi đã làm điều này hơi khác một chút, nhưng sử dụng khái niệm chỉ đặt ViewController đầu tiên và tải tất cả các động cơ khác. Làm tốt lắm! – Marquis103

Các vấn đề liên quan