2014-01-07 45 views
0

我使用2 ViewControllers創建基本TabBarController。 它的工作,但它有錯誤,我想要一些東西。如何更改UITabBarController的默認ViewController

問題1。

當我運行在TabBarSecondViewController(默認ViewController is FirstViewController)的名字沒有出現。

enter image description here

我想下面的這張圖片。

enter image description here

Problem2。 在我的代碼中,默認視圖是FirstViewController。 如果我想設置默認視圖是SecondViewController Same Tab Bar(FirstView Tab:Left,and SecondView Tab:Right) 下面這張圖片。如何解決它。

enter image description here

這是我的示例代碼

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    FirstViewController *firstVC = [[FirstViewController alloc] init]; 
    UINavigationController *firstNVC = [[UINavigationController alloc] initWithRootViewController:firstVC]; 
    firstNVC.navigationBar.barStyle = UIBarStyleBlack; 

    SecondViewController *secondVC = [[SecondViewController alloc] init]; 
    UINavigationController *secondNVC = [[UINavigationController alloc] initWithRootViewController:secondVC]; 
    secondNVC.navigationBar.barStyle = UIBarStyleBlack; 

    UITabBarController *tabController = [[UITabBarController alloc]init]; 
    tabController.navigationItem.hidesBackButton = NO; 
    tabController.viewControllers = [NSArray arrayWithObjects:firstNVC, secondNVC, nil]; 
    tabController.navigationController.navigationBarHidden = NO; 

    self.window.rootViewController = tabController; 
    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

謝謝。 ^^

回答

0
UIViewController *viewController1 = [[[viewController1 alloc] initWithNibName:@"viewController1" bundle:nil] autorelease]; 
    UIViewController *viewController2 = [[[viewController2 alloc] initWithNibName:@"viewController2" bundle:nil] autorelease]; 
    UIViewController *viewController3 = [[[viewController3 alloc] initWithNibName:@"viewController3" bundle:nil] autorelease]; 


    navControl1=[[UINavigationController alloc]initWithRootViewController:viewController1]; 
    navControl2=[[UINavigationController alloc]initWithRootViewController:viewController2]; 
    navControl3=[[UINavigationController alloc]initWithRootViewController:viewController3]; 

    navControl1.navigationBar.tintColor=[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8]; 
    navControl2.navigationBar.tintColor=[UIColor blackColor]; 
    navControl3.navigationBar.tintColor=[UIColor blackColor]; 

    self.tabBarController = [[[UITabBarController alloc] init] autorelease]; 
    self.tabBarController.delegate=self; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:navControl2,navControl1,navControl3, nil ]; 

    [[[[self.tabBarController tabBar] items] objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:@""] withFinishedUnselectedImage:[UIImage imageNamed:@""]]; 
    [[[[self.tabBarController tabBar] items] objectAtIndex:0] setTitle:@"1"]; 
    [[[[self.tabBarController tabBar] items] objectAtIndex:0] setImage:[UIImage imageNamed:@"tab2.png"]]; 
    [[[[self.tabBarController tabBar] items] objectAtIndex:1] setTitle:@"2"]; 
    [[[[self.tabBarController tabBar] items] objectAtIndex:1] setImage:[UIImage imageNamed:@"tab11.png"]]; 
    [[[[self.tabBarController tabBar] items] objectAtIndex:2] setTitle:@"3"]; 
    [[[[self.tabBarController tabBar] items] objectAtIndex:2] setImage:[UIImage imageNamed:@"tab5.png"]]; 

    [self.tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"trans.png"]]; 

    UIImage* tabBarBackground = [UIImage imageNamed:@""]; 
    [[UITabBar appearance] setBackgroundImage:tabBarBackground]; 
    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"trans.png"]]; 
+1

非常感謝你。 我使用[[[[[self.tabBarController tabBar] items] objectAtIndex:0] setTitle:@「1」]; ^^ –

0

問題2。如果我想 設置默認視圖是由相同的選項卡欄(FirstView標籤 :左和SecondView選項卡:右)的SecondViewController。如何 解決它。

解決方案:

[self.tabBarController setSelectedIndex:1]; 
+0

**問題2 **:我使用這段代碼[self.tabBarController setSelectedIndex:1];和[tabController.tabBarController setSelectedIndex:1];但它不起作用。 T^T –

0

問題1:

嘗試設置標題爲視圖控制器。

問題2:

你剛纔設置的tabBarController的選擇指數爲1,應該這樣做。

+0

**問題1 **:是的,我已經爲兩個ViewController設置了標題,但它不會發生。 **問題2 **:我使用這個代碼[self.tabBarController setSelectedIndex:1];和[tabController.tabBarController setSelectedIndex:1];但它不起作用。 T^T –

+0

mmmm只是一個問題,你爲什麼要通過代碼設置它?你爲什麼不使用故事板?!?這將解決您的所有問題:\ – Nour1991

+0

在我的項目中僅使用代碼...對不起T^T –

0

問題1

您沒有設置tabbarItem每個導航控制器,我想你應該設置與標題UITabBarItem第一,然後將其分配給tabbarItem財產導航控制器。

Problem2

正如@Kumar說。

+0

您能爲我顯示示例代碼嗎? –

+0

Like:UITabBarItem * item = [UITabBarItem alloc] initWithTitle:@「title」image:nil selectedImage:nil]; secondNVC.tabBarItem = item;對於Problem2,你最好在'[self.window makeKeyAndVisible]'之後調用'setSelectedIndex'方法;'' –

+0

這不是工作T^T –

相關問題