如何在iphone中的UITabbarController类上调用右键栏按钮项

时间:2011-05-19 10:28:22

标签: iphone objective-c uitabbarcontroller uinavigationitem

我已经以编程方式创建了一个继承自UITabBarController的tabbar类。

这是我的代码:

    #import <UIKit/UIKit.h>
    @class JourneyAppDelegate;

    @interface TTabBar :UITabBarController<UITabBarControllerDelegate> {
    UITabBarController *mTabController;
    JourneyAppDelegate *app;
    }


    #import "TTabBar.h"
    #import "TAddNewJourney.h"
    #import "TJourneylistController.h"
    #import "TAppStoreController.h"
    #import "TSettingsController.h"
    #import "TAboutController.h"
    #import "TReviewsController.h"
    #import "JourneyAppDelegate.h"

    @implementation TTabBar

    // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    /*
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization.
        }
        return self;
    }
    */

    /*
    // Implement loadView to create a view hierarchy programmatically, without using a nib.
    - (void)loadView {
    }
    */

    -(void)Add
    {

        app= (JourneyAppDelegate*)[[UIApplication sharedApplication]delegate];
        TAddNewJourney *j=[[TAddNewJourney alloc]init];
        [app.navigationController pushViewController:j animated:YES];
        [j release];



    }



    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {

        [super viewDidLoad];
        self.navigationController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonSystemItemDone target:self action:nil];

        TJourneylistController *journeylist =[[TJourneylistController alloc]initWithNibName:nil bundle:nil];
        //UINavigationController *journeylistnavigation =[[UINavigationController alloc]initWithRootViewController:journeylist];

        //this is to provide the right navigation button item
        UIBarButtonItem *addButton = [[[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                       target:self
                                       action:@selector(Add)] autorelease];
        self.navigationItem.rightBarButtonItem = addButton;

        [journeylist.tabBarItem initWithTitle:@"Journey List" image:[UIImage imageNamed:@"newJourney.png"] tag:1];
        journeylist.navigationItem.title =@"Journey List";
        //[journeylist release];

        TAppStoreController *appstore =[[TAppStoreController alloc]initWithNibName:nil bundle:nil];
        //UINavigationController *appstorenavigation =[[UINavigationController alloc]initWithRootViewController:appstore];
        [appstore.tabBarItem initWithTitle:@"App Store" image:[UIImage imageNamed:@"appStore.png"] tag:2];

        appstore.navigationItem.title = @"App Store";
        //[appstore release];

        TSettingsController *settings =[[TSettingsController  alloc]initWithNibName:nil bundle:nil];
        //UINavigationController *settingsnavigation =[[UINavigationController alloc]initWithRootViewController:settings];
        [settings.tabBarItem initWithTitle:@"Settings" image:[UIImage imageNamed:@"settings.png"] tag:3];
        settings.navigationItem.title = @"Settings";
        //[settings release];

        TAboutController *about =[[TAboutController alloc]initWithNibName:nil bundle:nil];
        //UINavigationController *aboutnavigation =[[UINavigationController alloc]initWithRootViewController:about];
        [about.tabBarItem initWithTitle:@"About" image:[UIImage imageNamed:@"contactUs.png"] tag:4];
        about.navigationItem.title =@"About Us";


    //  [about release];

        NSArray *controllers = [NSArray arrayWithObjects:journeylist,appstore,settings,about,nil];
        //self.viewControllers = controllers;
        self.viewControllers = controllers;
        //[controllers release];
    }



i need a right navigation item on the class Journeylistcontroller.So i have added it to the code but the problem is the navigtaion item gets set to the four controllers.i need that only journeylistcontroller should have the navigation item.Please can anybody help me in solving this problem.Thanks

1 个答案:

答案 0 :(得分:0)

如果只需要在一个视图中显示barbutton项,则为该视图控制器创建一个navigationController和Journeylistcontroller,然后将创建的navigationcontroller添加到该数组中。 此外,您需要将rightbarbutton项添加到创建的导航控制器而不是self.navigationController。 修改

TJourneylistController *journeylist =[[TJourneylistController alloc]initWithNibName:nil bundle:nil];

[journeylist.tabBarItem initWithTitle:@"Journey List" image:[UIImage imageNamed:@"newJourney.png"] tag:1];

        journeylist.navigationItem.title =@"Journey List";
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:journeylist];
                    navController.navigationBar.hidden=YES;


UIBarButtonItem *addButton = [[[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                       target:self
                                       action:@selector(Add)] autorelease];
        navController.navigationItem.rightBarButtonItem = addButton;

试试这个,同时将navController添加到数组而不是journeylist