iphone - 如何在 iPhone 中手动更改 TabItem?

标签 iphone delegates uitabbarcontroller

我正在使用TabbarController。当我位于第一个选项卡中时,所有其他选项卡项都处于禁用模式,并且我在第一个选项卡 View Controller 中有一个按钮,单击该按钮时,它应该移动到第二个选项卡项。我已经实现了 setselectedindex 但没有改变。

 - (void)clinicBtnTapped
 {        
    enteredSecondTab==YES;
    CustomTabBar *tabbar=[[CustomTabBar alloc] init];

    [self.tabBarController setSelectedIndex:1];

    AppSys_SplitView *apptmtSys=[[AppSys_SplitView alloc] init];

    [tabbar tabBarController:tabbar.tabBarController shouldSelectViewController:apptmtSys];
    tabindex=1;
    [tabbar tabBarController:tabbar.tabBarController didSelectViewController:appsys];

}

在应该选择ViewController

    - (BOOL)tabBarController:(UITabBarController *)tabBarControlle shouldSelectViewController:(UIViewController *)viewController{

   tabindex = [[tabBarControlle viewControllers] indexOfObject:viewController];
    NSLog(@"index %d",index);

    for(UIImageView *view in[self.view subviews]){

        [view removeFromSuperview];
    }
        tabBarController.imgV.frame=CGRectMake(0, 717, 1024, 53);

    switch (tabindex) {

        case 0:

            enteredFirstTab = YES;
            enteredSecondTab = NO;
            enteredThirdTab = NO;
            enteredFourthTab = NO;
            tabBarController.imgV.image=[UIImage imageNamed:@"select_TrackClinic_icon.png"];


            return YES;
            break;

        case 1:             
            if(enteredSecondTab == YES)
            {
                enteredFirstTab = NO;
                enteredSecondTab = YES;
                enteredThirdTab = NO;
                enteredFourthTab = NO;
                tabBarController.imgV.image=[UIImage imageNamed:@"select_Application_icon.png"];
                return YES;
            }
            else 
            {
                return YES;
            }



            break;


        case 2:

            return NO;

            enteredFirstTab = NO;
            enteredSecondTab = NO;
            enteredThirdTab = YES;
            enteredFourthTab = NO;
            tabBarController.imgV.image=[UIImage imageNamed:@"select_regularOnsite_icon.png"];

            break;

        case 3:

            return NO;

            enteredFirstTab = NO;
            enteredSecondTab = NO;
            enteredThirdTab = NO;
            enteredFourthTab = YES;
            tabBarController.imgV.image=[UIImage imageNamed:@"select_resultUpdate_icon.png"];
            break;

        default:

            break;

    }       

}

最佳答案

在这里我可以给你一些实现自定义标签栏的教程

Delegate.h 文件

@interface cTabBarAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> 
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet UIImageView *imgV;
@end

Delegate.m 文件

@implementation cTabBarAppDelegate

@synthesize window=_window;
@synthesize tabBarController=_tabBarController;
@synthesize imgV = _imgV;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    // Add the tab bar controller's current view as a subview of the window
    self.tabBarController.delegate=self;
    self.imgV.frame=CGRectMake(0, 425, 320, 55);
    [self.tabBarController.view addSubview:self.imgV];
    self.tabBarController.selectedIndex=0;
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];

    switch (index) {
        case 0:
            self.imgV.image=[UIImage imageNamed:@"tBar1.png"];
            break;
        case 1:
            self.imgV.image=[UIImage imageNamed:@"tBar2.png"];
            break;
        case 2:
            self.imgV.image=[UIImage imageNamed:@"tBar3.png"];
            break;
        case 3:
            self.imgV.image=[UIImage imageNamed:@"tBar4.png"];
            break;
        case 4:
            self.imgV.image=[UIImage imageNamed:@"tBar5.png"];
            break;
        default:
            break;
    }

    return YES;
}

以下代码放入viewcontroller.m

  AppDelegate   *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

    SearchVctr *viewController1 = [[SearchVctr alloc] initWithNibName:@"SearchVctr" bundle:nil];
    UINavigationController *navForRentResidental= [[UINavigationController alloc]initWithRootViewController:viewController1];

    UIViewController *viewController2 = [[FavouriteVctr_iphone alloc] initWithNibName:@"FavouriteVctr_iphone" bundle:nil];
    UINavigationController *navForFavorite= [[UINavigationController alloc]initWithRootViewController:viewController2];

    UIViewController *viewController3 = [[LoginAndRegisterVctr alloc] initWithNibName:@"LoginAndRegisterVctr" bundle:nil];
    UINavigationController *navForLogin= [[UINavigationController alloc]initWithRootViewController:viewController3];
    navForLogin.navigationBar.hidden=TRUE;    

    UIViewController *viewController4 = [[ContactUsVctr alloc] initWithNibName:@"ContactUsVctr" bundle:nil];
    UINavigationController *navForContact= [[UINavigationController alloc]initWithRootViewController:viewController4];

    UIViewController *viewController5 = [[MoreVctr_iphone alloc] initWithNibName:@"MoreVctr_iphone" bundle:nil];
    UINavigationController *navForMore= [[UINavigationController alloc]initWithRootViewController:viewController5];

    navForContact.navigationBar.hidden=TRUE;
    navForMore.navigationBar.hidden=TRUE;
    navForLogin.navigationBar.hidden=TRUE;
    navForFavorite.navigationBar.hidden = TRUE;

    if([sender tag] == 1){
        delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navForRentResidental, navForFavorite, navForLogin, navForContact, navForMore, nil];
        delegate.tabBarController.selectedIndex = 3;
        delegate.imgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"t4.png"]];
        delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:delegate.tabBarController animated:YES];
    }else if([sender tag] == 2){
        UIViewController *viewController5 = [[AboutVctr alloc] initWithNibName:@"AboutVctr" bundle:nil];
        delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navForRentResidental, navForFavorite, navForLogin, navForContact, viewController5, nil];
        delegate.tabBarController.selectedIndex = 4;
        delegate.imgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"t5.png"]];
        delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:delegate.tabBarController animated:YES];
    }
        else if([sender tag] == 3){
        UIViewController *viewController5 = [[ServicesVctr alloc] initWithNibName:@"ServicesVctr" bundle:nil];
        UINavigationController *navcontroller= [[UINavigationController alloc]initWithRootViewController:viewController5];
        navcontroller.navigationBar.hidden=TRUE;
        delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navForRentResidental, navForFavorite, navForLogin, navForContact, navcontroller, nil];
        delegate.tabBarController.selectedIndex = 4;
    delegate.imgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"t5.png"]];
        delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:delegate.tabBarController animated:YES];
    }
    else if([sender tag] == 4){
        delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navForRentResidental, navForFavorite, navForLogin, navForContact, navForMore, nil];
        delegate.tabBarController.selectedIndex = 2;
        delegate.imgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"t3.png"]];
         delegate.imgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"t3.png"]];
        delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:delegate.tabBarController animated:YES];
    }
        else if([sender tag] == 5){
        delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navForRentResidental, navForFavorite, navForLogin, navForContact, navForMore, nil];
        delegate.tabBarController.selectedIndex = 0;
        delegate.imgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"t1.png"]];
        delegate.imgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"t1.png"]];
                        delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:delegate.tabBarController animated:YES];

    }

此代码可能有助于开发。

关于iphone - 如何在 iPhone 中手动更改 TabItem?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11521339/

相关文章:

两个 Controller 之间的 iOS 对象或委托(delegate)?

c# - 何时使用或不使用 Lambda 表达式

C# GetFunctionPointerForDelegate cdecl 而不是 stdcall

ios - 为不是 TabBar 项目但在菜单中的项目显示主 TabBar(使用 SWRevealCotroller)

ios - 如何在我的选项卡栏应用程序 (iPad) 中创建 UISplitViewController?

iphone - ios 中的 NSDate 转换

iphone - 如何在运行时更改 iPhone 应用程序首选项设置?

ios - UITabbar项目objective-c中的颜色图标

ios - 应用仅在装有iOS 7.1的iPhone 4中挂起

iphone - 我在哪里可以找到 iPhone 上 opencv 的 xcode 模板?