簡體   English   中英

iOS 7:標簽欄控制器的不同導航項

[英]iOS 7: different navigation items for tab bar controller

我對iOS應用程序開發相對較新。 目前我正在開發一個帶標簽欄的小應用程序。 我面臨的問題是我希望在foreach選項卡中有不同的導航項。 我嘗試了很多東西,但事情都沒有用。 我用原生iOS語言編程。

在我的應用程序中,我有一個AppDelegate。 在我的AppDelegate中,有一小段代碼用於設置我的mainViewController:

- (void)setupOverlordViewController
{
    MainViewController *rootVC = [[MainViewController alloc] initWithNibName:nil bundle:nil];
    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
    self.window.rootViewController = navVC;
}

我在MainViewController中設置我的標簽:

- (void)viewDidLoad
{
    UIViewController *tabView1 = [[Tab1ViewController alloc] init];
    UIViewController *tabView2 = [[Tab2ViewController alloc] init];
    UIViewController *tabView3 = [[Tab3ViewController alloc] init];

    NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
    [tabViewControllers addObject:tabView1];
    [tabViewControllers addObject:tabView2];
    [tabViewControllers addObject:tabView3];

    [self setViewControllers:tabViewControllers];

    tabView1.tabBarItem =
    [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"TabView1", nil)
                                  image:[UIImage imageNamed:@"tabView1.png"]
                                    tag:1];

    tabView2.tabBarItem =
    [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"TabView2", nil)
                                  image:[UIImage imageNamed:@"tabView2.png"]
                                    tag:2];
    tabView3.tabBarItem =
    [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"TabView3", nil)
                                  image:[UIImage imageNamed:@"tabView3.png"]
                                    tag:3];
}

每個視圖(tabView1,tabView2,tabView3)都有自己的布局,在View的ViewDidLoad方法中設置。 當我想通過在ViewDidLoad方法中添加導航按鈕時在導航欄中添加導航按鈕,但似乎無法添加按鈕。 添加它們的唯一方法是直接在我的MainViewController中,但是我不能設置導航欄按鈕不同的foreach選項卡。

將按鈕添加到導航欄的代碼如下:

UIBarButtonItem *btnNewRecord = [[UIBarButtonItem alloc]     initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self      action:@selector(btnNewRecord)];


NSArray *rightItems = [NSArray arrayWithObjects:btnNewRecord, nil];
[self.navigationItem setRightBarButtonItems:rightItems];

有人可以解釋一下我做錯了什么嗎?

我已經使用xib文件為您創建了一個示例。 我創建了三個視圖控制器並將它們添加到導航控制器。 appdelegate代碼之后:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
    UINavigationController *firstNavVC = [[UINavigationController alloc] initWithRootViewController: firstVC];

    SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
    UINavigationController *secondNavVC = [[UINavigationController alloc] initWithRootViewController: secondVC];

    ThirdViewController *thirdVC = [[ThirdViewController alloc] initWithNibName:@"ThirdView" bundle:nil];
    UINavigationController *thirdNavVC = [[UINavigationController alloc] initWithRootViewController: thirdVC];

    NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
    [tabViewControllers addObject:firstNavVC];
    [tabViewControllers addObject:secondNavVC];
    [tabViewControllers addObject:thirdNavVC];

    firstNavVC.tabBarItem =
    [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"First", nil)
                                  image:nil
                                    tag:1];

    secondNavVC.tabBarItem =
    [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"Second", nil)
                                  image:nil
                                    tag:2];
    thirdNavVC.tabBarItem =
    [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"Third", nil)
                                  image:nil
                                    tag:3];

    UITabBarController *tabbarController = [[UITabBarController alloc] init];
    tabbarController.viewControllers = tabViewControllers;

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

    [self.window makeKeyAndVisible];

    return YES;
}

以下是輸出:

在此輸入圖像描述在此輸入圖像描述在此輸入圖像描述

您可以在此處下載代碼示例

您需要為每個標簽欄視圖控制器單獨的導航控制器,然后您可以在每個導航控制器上添加UIBarButtonItem。

暫無
暫無

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

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