iPhone中的标签栏控制器 [英] Tab Bar controller in iPhone

查看:91
本文介绍了iPhone中的标签栏控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中添加了两个标签,使用这些标签加载两个视图控制器

I have added Two Tabs in my app, to load two view controller using these tabs


  • Tab1:Home

  • Tab2:收藏

所以我写了下面的代码来实现这个

so I have written below code to achieve this

在app appate

In app Delegate

AppDelegate.h

@class ViewController,FavViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;
@property (strong, nonatomic) FavViewController *favViewController;

@property (strong, nonatomic) UITabBarController *tabBarController;

@end

AppDelegate.m

       @implementation AppDelegate

        @synthesize window = _window;
        @synthesize viewController;
        @synthesize favViewController;
        @synthesize tabBarController;

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

        baseTabBarController = [[UITabBarController alloc]init];

        baseTabBarController.delegate=self;

        viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
        UINavigationController *homeView = [[UINavigationController alloc]initWithRootViewController:viewController];  

        favViewController = [[FavViewController alloc] initWithNibName:@"FavViewController" bundle:nil];
        favViewController.title = @"My Favourite";
        UINavigationController *favouriteView = [[UINavigationController alloc] initWithRootViewController:favViewController];  


        NSArray *controllers = [NSArray arrayWithObjects:homeView,favouriteView, nil];  
        baseTabBarController.viewControllers = controllers; 

        [self.window addSubview:baseTabBarController.view]; 
        [self.window makeKeyAndVisible];    

        return YES;
    }

    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)selectedViewController
{

    if (tabBarController.selectedIndex == 0) {




   }else if (tabBarController.selectedIndex == 1) {

    [(FavViewController *)[(UINavigationController*)selectedViewController topViewController] getData];

    }else if (tabBarController.selectedIndex == 2) {

        NSLog(@"2");
    }


}

这里是我得到的结果屏幕

and here is the Result Screen I am getting

所以我在这方面遇到麻烦..

如果我切换到下一个屏幕我的第一个选项卡没有加载第一个屏幕(主屏幕)而只是停留在当前屏幕上。

If I switch to the next screen my First tab doesn't load First Screen (Home screen) instead just stay on current screen.

让我试试示例


我的应用程序中有四个屏幕,请说A,B,C,D

There are four screens in my app let say A, B, C, D

我有为A和C屏幕添加了标签,这些标签在整个应用程序中可用
(全屏)。

I have added Tabs for A and C screens,those are available in whole app (All screen).

现在,如果我将启动应用程序并转到A - > B-> C - > D并按住主页选项卡(A)
它不会加载屏幕A,而只是停留在当前屏幕上

Now if I'll start app and go A ->B->C -> D and press on Home Tab (A) it doesn't load Screen A, instead just stay on Current screen

但是很好事情是,如果我用另一个标签C(我的
收藏)进行相同的处理,它会加载正确的屏幕。

but good thing is if i do same process with another tab C (My Favourite) it loads correct screen.

编辑:我已经实现了 didSelectViewController 方法,因为@sanjit建议我但是我无法区分,这次点击了哪个标签?

I have implemented didSelectViewController method as @sanjit suggested me but in that I not able distinguish, which tab is tapped this time?

我真的很感激!!如果有人能指出正确的方向

I would be really appreciate!! if someone can point me on right direction

推荐答案

使用Tabbar委托并使用uiviewcontroller的参数实例调用poptorootviewcontroller方法。希望它可能对你有用。尝试以下代码

Use Tabbar delegate and using parameter instance of uiviewcontroller call poptorootviewcontroller method. Hope it may work for you. Try the following code

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
UINavigationController *navController = (UINavigationController*)tabBarController.selectedViewController;

[navController popToRootViewControllerAnimated:YES];
}

这篇关于iPhone中的标签栏控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆