2011-09-06 15 views
6

Chcę programowo ustawić kontroler paska kart i kontroler nawigacyjny. Mój kod działa tak daleko, że pokazuje pasek kart na dole, ale OptionViewController nic nie mówi (bez tytułu) na przycisku drugiego paska kart. Zabawne jest to, że gdy klikam przycisk bez niczego na nim, pojawia się tytuł (i taki jest jego pogląd), czy ktoś może mi wyjaśnić, co robię źle? Próbowałem użyć następującego kodu:Programowanie programowania TabBarController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  
    // Override point for customization after application launch. 
    [self.window makeKeyAndVisible]; 

    NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2]; 

    DefaultViewController *dvc = [[DefaultViewController alloc] init]; 
    UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc]; 
    [tabItems addObject:dvc_nc]; 
    [dvc release]; 
    [dvc_nc release]; 

    OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped]; 
    UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc]; 
    [tabItems addObject:ovc_nc]; 
    [ovc release]; 
    [ovc_nc release]; 

    UITabBarController *tbc = [[UITabBarController alloc] init]; 
    tbc.viewControllers = tabItems; 
    self.tabController = tbc; 
    [tabItems release]; 
    [tbc release]; 

    [self.window addSubview:self.tabController.view]; 

    return YES; 
} 
+0

Chyba trzeba dodać UINavigationController jako widok w sub Kontroler Tab Bar z kontrolującymi klasami super klasy jako UINavigationController –

+0

problemem jest tylko brakujący tytuł, prawda? gdzie ustawiasz 'title' swojego' OptionsViewConbtroller'? Jeśli ustawiasz tytuł nie w swojej "init" -metodzie, to TabBarController odczytuje tylko pusty tytuł z OptionsVC. Domyślam się, że ustawiasz własności tytułu w czymś. jak 'viewDidLoad'? – thomas

+0

Nie sądzę, ponieważ: [tbc.view addSubview: ovc_nc.view]; sprawia, że ​​ekran jest całkowicie pusty! – Mark

Odpowiedz

10

Musisz ustawić tabBarItem i tytuł UINavigationController a nie jego głównym viewController.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  
    // Override point for customization after application launch. 
    [self.window makeKeyAndVisible]; 

    NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2]; 

    DefaultViewController *dvc = [[DefaultViewController alloc] init]; 
    UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc]; 
    dvc_nc.tabBarItem.title = @"Default"; 
    dvc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"]]; 
    [tabItems addObject:dvc_nc]; 
    [dvc release]; 
    [dvc_nc release]; 

    OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped]; 
    UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc]; 
    ovc_nc.tabBarItem.title = @"Option" 
    ovc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Optiomn" ofType:@"png"]]; 

    [tabItems addObject:ovc_nc]; 
    [ovc release]; 
    [ovc_nc release]; 

    UITabBarController *tbc = [[UITabBarController alloc] init]; 
    tbc.viewControllers = tabItems; 
    self.tabController = tbc; 
    [tabItems release]; 
    [tbc release]; 

    [self.window addSubview:self.tabController.view]; 

    return YES; 
} 
+0

Czy masz kod, który pokazuje dokładnie, co masz na myśli? – Mark

+0

Zaktualizowałem swój wpis, pokaże ci on jak ustawić tabBarItem. – rckoenes

+0

Teraz poprawnie pokazuje tytuł, ale teraz, gdy ustawiam tytuł w widoku opcji na "Opcje" i twoją metodą uruchamiam go w "Opcji", więc po uruchomieniu aplikacji widzę "Domyślne" i "Opcja". Po kliknięciu elementu TabBar zmienia on tytuł na to, co skonfigurowałem w viewDidLoad z self.title! – Mark

0

Jeśli ktoś potrzebuje wersji SWIFT. To działało dla mnie. Dzięki @rckoenes za odpowiedź na WKC, z której tłumaczę to.

window?.makeKeyAndVisible() 

    let dvc = HomeViewController() 
    let dvc_nc = UINavigationController(rootViewController: dvc) 
     dvc_nc.tabBarItem.title = "Home" 
     dvc_nc.tabBarItem.image = UIImage(named: "HomeIcon") 
    controllers.append(dvc_nc) 

    let ovc = ProfileViewController() 
    let ovc_nc = UINavigationController(rootViewController: ovc) 
     ovc_nc.tabBarItem.title = "Profile" 
     ovc_nc.tabBarItem.image = UIImage(named: "ProfileIcon") 
    controllers.append(ovc_nc) 

    let tbc = UITabBarController() 
     tbc.viewControllers = controllers 

    window?.rootViewController = tbc 

    UINavigationBar.appearance().tintColor = UIColor(red: 0.05, green: 0.47, blue: 0.91, alpha: 1.0) 
    UINavigationBar.appearance().barTintColor = UIColor(red: 0.05, green: 0.47, blue: 0.91, alpha: 1.0) 
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()] 
    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent 
3

stworzyłem UITabbarController jako rooview kontroler aplikacji z UINavigationController dla UIViewController.

tutaj jeszcze jeden przykład: Użyłem xibs dla kontrolerów widoku.

AppDelegate.m

utworzyć nazwę metoda: setupAppHome

#pragma mark - SETUP HOME 
-(void) setupAppHome{ 
    NSLog(@"set up the nano home"); 

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

    if (_chatViewController == nil) { 
     _chatViewController = [[ChatViewController alloc] initWithNibName:@"ChatViewController" bundle:nil]; 
     chatNav = [[UINavigationController alloc] initWithRootViewController:_chatViewController]; 
     [email protected]"Chat"; 
     chatNav.tabBarItem.image=[UIImage imageNamed:@"chat_icon.png"]; 

    } 
    if (_callController == nil) { 
     _callController = [[CallViewController alloc] initWithNibName:@"CallViewController" bundle:nil]; 
     callNav = [[UINavigationController alloc] initWithRootViewController:_callController]; 
     [email protected]"Call"; 
     callNav.tabBarItem.image=[UIImage imageNamed:@"call_icon.png"]; 

    } 
    if (_contanctsController == nil) { 
     _contanctsController = [[ContactsViewController alloc] initWithNibName:@"ContactsViewController" bundle:nil]; 
     conNav = [[UINavigationController alloc] initWithRootViewController:_contanctsController]; 
     [email protected]"Contact"; 
     conNav.tabBarItem.image=[UIImage imageNamed:@"contact_icon.png"]; 

    } 
    if (_settingController == nil) { 
     _settingController = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil]; 
     settingNav = [[UINavigationController alloc] initWithRootViewController:_settingController]; 
     [email protected]"Setting"; 
     settingNav.tabBarItem.image=[UIImage imageNamed:@"setting_icon.png"]; 

    } 

    self.tabController = [[UITabBarController alloc] init]; 

    NSMutableArray   *controllers = [[NSMutableArray alloc] initWithCapacity:4]; 
    [controllers addObject:chatNav]; 
    [controllers addObject:callNav]; 
    [controllers addObject:conNav]; 
    [controllers addObject:settingNav]; 


    self.tabController.viewControllers = controllers;//@[chatNav,callNav,conNav,settingNav]; 

    self.tabController.selectedIndex=0; 



    [self.window setRootViewController:self.tabController]; 
    [self.window makeKeyAndVisible]; 


} 

Jest TextEd w Xcode 9 z iOS 11.