2013-06-11 31 views
6

Szukałem programowego dodawania paska kart do kontrolera widoku, ponieważ widok przewijania nie mogę go włączyć, nie będąc w środku mojego widoku. Nie jestem zdezorientowany, jak to dodać. Czy trzeba zainicjować moją metodę ViewDidLoad lub moją AppDelegate? Jeśli mam:Programowe tworzenie paska zakładek dla ViewController

UITabBarController *tabBar = [[UITabBarController alloc] init]; 
[self.view addSubview:tabBar.view]; 
[tabBar release]; 

Jak mogę przydzielić go do głębi scrollview?

Dzięki!

Teraz w mojej klasie appDelegate:

 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    UITabBarController *tabBarController = [[UITabBarController alloc] init]; 

    ViewController* vc = [[ViewController alloc] init]; 


    NSArray* controllers = [NSArray arrayWithObjects:vc, tabBarController, nil]; 
    tabBarController.viewControllers = controllers; 

    [_window addSubview:tabBarController.view]; 

    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 

} 

To upaść i nie wiem, czy to uwolnienie mi brakuje.

Edit: dla nikogo innego zastanawiać to w Appdelegate.m:

self.tabBarController = [[[UITabBarController alloc] init] autorelease]; 
self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4]; 
+0

'Kontrolery NSArray * = [NSArray arrayWithObjects: vc, tabBarController, nil];' Dlaczego dodajesz 'tabBarController'? – Bhavin

+0

Tak, dodałem Edytuj w moim pytaniu, aby pokazać, co zadziałało. –

+0

Było jeszcze jedno i było: Dodałeś 'tabBarController' w tablicy' viewControllers' i to było złe. Właśnie to dodałem w mojej odpowiedzi. – Bhavin

Odpowiedz

7

przyjrzeć się tej dokumentacji określonej przez Apple: ViewControllers.

Przykładowy kod:

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    UITabBarController *tabBarController = [[UITabBarController alloc] init]; 

    FirstViewController* vc1 = [[FirstViewController alloc] init]; 
    SecondViewController* vc2 = [[SecondViewController alloc] init]; 

    NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil]; 
    tabBarController.viewControllers = controllers; 

    // Add the tab bar controller's current view as a subview of the window 
    [window addSubview:tabBarController.view]; 
} 
+0

tylko jedna mała rzecz: '' '[self.window addSubview: tabBarController.view];' '' – Ben

3

zrobiłem to w ten sposób. Skopiowałem to z mojego App Delegata i działa dobrze. Zasadniczo dodajesz kontrolki widoku do paska kart, a następnie dodajesz pasek kart do podglądu okna.

instancji instancja

iVacationTabBar = [[UITabBarController alloc] init]; 

jednak utworzyć widoki/view regulatorów:

UINavigationController *rvc_tools = [[UINavigationController alloc] initWithRootViewController: vc_tools]; 
    UINavigationController *rvc_settings = [[UINavigationController alloc] initWithRootViewController: vc_settings]; 
    UINavigationController *rvc_about = [[UINavigationController alloc] initWithRootViewController: vc_about]; 
    UINavigationController *rvc_currentpage = [[UINavigationController alloc] initWithRootViewController: vc_currentpage]; 
    UINavigationController *rvc_backup = [[UINavigationController alloc] initWithRootViewController:vc_backup]; 

dodać kontrolery do tablicy:

NSArray* controllers = [NSArray arrayWithObjects: rvc_currentpage, rvc_tools,rvc_settings, rvc_backup, rvc_about, nil]; 

Ustaw tablicę widzenia kontrolerów do paska kart:

[iVacationTabBar setViewControllers: controllers animated:NO]; 

Dodaj pasek kart do podglądu okna.

[_window addSubview:iVacationTabBar.view]; 
0

nie można dodać UITabbarController na UIViewController. Zamiast tego, na ViewController musisz pokazać TabbarController, utworzyć dla niego TabbarController, ustawić ViewController i dodać go do Window.

Powiązane problemy