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

发布于 2024-11-08 13:19:14 字数 4370 浏览 0 评论 0原文

我以编程方式创建了一个继承自 UITabBarController 的选项卡栏类。

这是我的代码:

    #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

I Have programatically created a tabbar class which inherits from UITabBarController.

This is my code:

    #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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2024-11-15 13:19:14

如果您只需要在一个视图中显示栏按钮项,则为该视图控制器创建一个 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添加到数组而不是旅程列表中

if you need to display barbutton item in only one view, then create a navigationController and Journeylistcontroller to that view controller and then, add created navigationcontroller to the array.
Also, u need to add rightbarbutton item to the created navigation controller not to self.navigationController.
Edit:

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;

try this , also add navController to array not journeylist

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文