2013-07-31 40 views
6

Trong Đại biểu của tôi, tôi đang cố gắng chọn TabBarController để tôi có thể tạo kiểu cho nó với một nền tảng khác. Tuy nhiên vấn đề là tôi TabBarController không nằm trên rootView ..Lấy thanh điều khiển từ chế độ xem khác trên bảng phân cảnh của tôi

mã hiện tại của tôi:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; 
UITabBar *tabBar = tabBarController.tabBar; 
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0]; 
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1]; 

Trong xây dựng giao diện của tôi đã thiết lập của tôi TabBarController với một tên Segue: mainView (Đây là nơi TabBarController nằm).

Làm cách nào tôi có thể chọn số TabBarController?

+0

trong đó một phần của mã bạn có muốn chọn tabBarController của mình không? Trong một UIViewController bên trong TabBar, APPDelegate, ...? –

+0

Yea trên dòng đầu tiên của tôi, tôi muốn thay thế 'self.window.rootViewController;' bằng TabBarController để tôi có thể thao tác nó – Alosyius

+0

Yea, nhưng tôi muốn biết trong phương thức ViewController/bạn đang gọi đoạn mã mà bạn chỉ đã đăng –

Trả lời

9

Trước tiên, bạn phải biết trong cấu trúc phân cấp chế độ xem của mình, nơi TabBarController của bạn ở đâu. Nếu nó không phải là bộ điều khiển gốc của bạn, Xác định vị trí UIViewController được gọi là TabBarController, và nhận được nó tham chiếu bởi segue hoặc một cái gì đó như nó.

Điều gì có thể hiệu quả với bạn, nó truy cập thuộc tính tabBarController trong số viewDidLoad của con đầu tiên UIViewController trong tab bên trong tabViewController của bạn. Tất cả con ViewControllers của tabBarController có thuộc tính này.

Ví dụ, giả sử UIViewController đầu tiên hiển thị trong TabBar được MyViewController, thực hiện một cái gì đó như thế này:

- (void)viewDidLoad 
{ 
    UITabBar *tabBar = self.tabBarController.tabBar; 
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0]; 
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1]; 
} 
5

Nếu bạn muốn nhận được nó từ một trong những điểm

//if Custom class 
TabBarController *tabBar = (TabBarController *) self.tabBarController; 

//if Custom class with Navigation Controller 
TabBarController *tabBar = (TabBarController *) self.navigationController.tabBarController; 

//if Not Subclassed 
UITabBarController *tabBar = (UITabBarController *) self.tabBarController; 

//if Not Subclassed with Navigation Controller 
UITabBarController *tabBar = (UITabBarController *) self.navigationController.tabBarController; 
Các vấn đề liên quan