1

我在我的应用程序中添加了两个选项卡,以使用这些选项卡加载两个视图控制器

  • Tab1 : 主页
  • Tab2 : 收藏夹

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

在应用程序代表

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");
    }


}

这是我得到的结果屏幕

在此处输入图像描述

所以我在哪里遇到麻烦..

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

让我举个例子

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

我为 A 和 C 屏幕添加了选项卡,它们在整个应用程序(所有屏幕)中都可用。

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

但好消息是,如果我对另一个选项卡 C(我的最爱)执行相同的过程,它会加载正确的屏幕。

编辑:我已经按照@sanjit 的建议实现了didSelectViewController方法,但是我无法区分,这次点击了哪个选项卡?

我将不胜感激!如果有人能指出我正确的方向

4

3 回答 3

3

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

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

[navController popToRootViewControllerAnimated:YES];
}
于 2013-01-23T12:19:33.200 回答
0

替换代码

[viewController setTabBarItem:tab1];
[favViewController setTabBarItem:tab2]; 

[[tabbarController tabBar] setItems:[NSArray arrayWithObjects:tab1,tab2, nil]];

那应该解决。

于 2013-01-23T12:09:14.387 回答
0
    Viewcontroller.h

    @property(nonatomic, retain) UITabBarItem *item1;
    @property(nonatomic, retain) UITabBarItem *item2;
    @property(nonatomic, retain) UITabBarItem *item3;
    @property(nonatomic, retain) UITabBarItem *item4;
    @property(nonatomic, retain) IBOutlet UITabBar *tabBar;


    Viewcontroller.m

    @synthesize item1,item2,item3,item4,tabBar;



- (void)viewDidLoad
{
     item1 = [[UITabBarItem alloc] initWithTitle:@"Home" image:[UIImage imageNamed:@"home.png"] tag:0];
      item2  = [[UITabBarItem alloc] initWithTitle:@"My Lists" image:[UIImage imageNamed:@"mylist"] tag:1];

     item3  = [[UITabBarItem alloc] initWithTitle:@"Search" image:[UIImage imageNamed:@"search.png"] tag:2];
     item4  = [[UITabBarItem alloc] initWithTitle:@"Settings" image:[UIImage imageNamed:@"setting.png"] tag:3];

     NSArray *items = [NSArray arrayWithObjects:item1,item2,item3,item4, nil];
        [tabBar setItems:items animated:NO];
        [tabBar setSelectedItem:[tabBar.items objectAtIndex:2]];
        tabBar.delegate=self;
        [self.view addSubview:tabBar

];
}


/****Function Call******/

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{

    if(item.tag == 0)
    {
        Newsfeeds *obj = [[Newsfeeds alloc]initWithNibName:@"Newsfeeds" bundle:Nil];

        [self.navigationController pushViewController:obj animated:NO];


    }


    else if(item.tag == 1)

    {

        MyLists *obj = [[MyLists alloc]initWithNibName:@"MyLists" bundle:Nil];

        [self.navigationController pushViewController:obj animated:NO];


    }

    else if(item.tag == 3)

    {

        Settings *obj = [[Settings alloc]initWithNibName:@"Settings" bundle:Nil];

        [self.navigationController pushViewController:obj animated:NO];

    }


}
于 2014-05-02T07:30:28.523 回答