iOS 7: different navigation items for tab bar cont

2019-05-02 11:57发布

I am relative new with the iOS app development. Currently I am developing a small app with a tab bar. The problem I am facing is that I would like to have different navigation items foreach tab. I tried a lot of things, but things aren't working. I am programming in the native iOS language.

In my app I've got a AppDelegate. In my AppDelegate there is a little piece of code for setting up my mainViewController:

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

I am setting up my tabs in my 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];
}

Each View (tabView1, tabView2, tabView3) has there own layout, which is set in the ViewDidLoad method of the View. When I would like to add navigation buttons in the navigation bar by adding them in the ViewDidLoad method, but it seems impossible to add the buttons. The only way to add them is directly in my MainViewController, but then I can't set the navigation bar buttons different foreach tab.

The code for adding the buttons to my navigation bar is as follows:

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


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

Could somebody explain me what I am doing wrong?

2条回答
beautiful°
2楼-- · 2019-05-02 12:10

I have created a example for you by using xib files. I have created three View Controllers and added them to navigation controllers. Following the appdelegate code :

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

Following is the output :

enter image description here enter image description here enter image description here

You can download code example here

查看更多
ゆ 、 Hurt°
3楼-- · 2019-05-02 12:32

you need separate navigation controller for each tab bar view controller & then you can add UIBarButtonItem on each navigation controller.

查看更多
登录 后发表回答