簡體   English   中英

如何更改UITabBarController的默認ViewController

[英]How to change default ViewController of UITabBarController

我用2個ViewControllers創建基本的TabBarController 它有效,但是有錯誤,我想要一些有關它的信息。

問題1。

當我運行(默認ViewController is FirstViewController )時,不會出現TabBarSecondViewController名稱。

在此處輸入圖片說明

我想看這張照片。

在此處輸入圖片說明

問題2。 在我的代碼中,默認視圖是FirstViewController 如果我想設置默認視圖,則此圖片SecondViewController的相同標簽欄( FirstView標簽:左, SecondView標簽:右)是SecondView 如何解決。

在此處輸入圖片說明

這是我的示例代碼

- (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;
}

謝謝。 ^^

問題2。 如果我要設置默認視圖,則使用Same Tab Bar(FirstView Tab:左,SecondView Tab:右)。 如何解決。

解:

[self.tabBarController setSelectedIndex:1];

問題一:

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

問題2:

您只需將tabBarController的選定索引設置為1,就應該這樣做。

問題1

您尚未為每個導航控制器設置tabbarItem ,我認為您應該先設置一個帶有標題的UITabBarItem ,然后將其分配給導航控制器的tabbarItem屬性。

問題2

正如@Kumar所說。

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"]];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM