在iPhone中推送UIViewController时如何更改tabbar项的选定索引

时间:2013-01-24 11:44:10

标签: iphone objective-c uitabbarcontroller uitabbaritem

我正在将一个视图控制器推送到另一个视图控制器但是当我按下tabbar项时索引没有改变。如何在将视图从一个推送到另一个时更改所选索引。提前感谢。

4 个答案:

答案 0 :(得分:6)

尝试,

[tabBar setSelectedItem:[[tabBar items] objectAtIndex:index]];

[tabBarController setSelectedIndex:index];

答案 1 :(得分:5)

此行可以帮助某人

<强> [self.parentViewController.tabBarController setSelectedIndex:0];

答案 2 :(得分:1)

试试这个:

    [self.tabBarController setSelectedIndex:0];

这里索引0引用tabbarcontroller的第一个标签。

希望它有所帮助.......

答案 3 :(得分:0)

UITabBarController的简单示例。只需创建三个UIViewController。此代码可以正常工作:)

<强> appDelegate.h

#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#import "ViewController.h"
#import "SecoundViewController.h"


@interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate,UINavigationControllerDelegate>
{
    FirstViewController *fView;
    SecoundViewController *sView;
    ViewController *viewCon;

    UITabBarItem *tbItem1;
    UITabBarItem *tbItem2;
    UITabBarItem *tbItem3;

     UINavigationController *FnavCon;
     UINavigationController *SnavCon;
     UINavigationController *navCon;

     UITabBarController *tbCon;
}

@property (strong, nonatomic) UIWindow *window;
@property(nonatomic,retain)FirstViewController *fView;
@property(nonatomic,retain)SecoundViewController *sView;
@property(nonatomic,retain)UINavigationController *navCon;
@property(nonatomic,retain)UITabBarController *tbCon;
@property(nonatomic,retain)ViewController *viewCon;
@property(nonatomic,retain)UINavigationController *FnavCon;
@property(nonatomic,retain)UINavigationController *SnavCon;
@property(nonatomic,retain)UITabBarItem *tbItem1;
@property(nonatomic,retain)UITabBarItem *tbItem2;
@property(nonatomic,retain)UITabBarItem *tbItem3;

@end

<强> appDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize fView,sView,navCon,tbCon,viewCon,FnavCon,SnavCon,tbItem1,tbItem2,tbItem3;

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

    self.viewCon=[[ViewController alloc] init];
    self.navCon=[[UINavigationController alloc] initWithRootViewController:self.viewCon];
    self.navCon.navigationBar.tintColor=[UIColor blackColor];
    self.viewCon.title=@"First View";

    self.fView=[[FirstViewController alloc] init];
    self.FnavCon=[[UINavigationController alloc] initWithRootViewController:self.fView];
    self.FnavCon.navigationBar.tintColor=[UIColor blackColor];

    self.fView.title=@"Secound View";

    self.sView=[[SecoundViewController alloc] init];
    self.SnavCon=[[UINavigationController alloc] initWithRootViewController:self.sView];
    self.SnavCon.navigationBar.tintColor=[UIColor blackColor];
    self.sView.title=@"Third View";

    UIImage *img1=[UIImage imageNamed:@"Australia.gif"];
    self.tbItem1=[[UITabBarItem alloc] initWithTitle:@"First Page" image:img1 tag:1];
    self.viewCon.tabBarItem=self.tbItem1;

    UIImage *img2=[UIImage imageNamed:@"Cameroon.gif"];
    self.tbItem2=[[UITabBarItem alloc] initWithTitle:@"Secound Page" image:img2 tag:2];
    self.fView.tabBarItem=self.tbItem2;

    UIImage *img3=[UIImage imageNamed:@"Canada.png"];
    self.tbItem3=[[UITabBarItem alloc] initWithTitle:@"Third Page" image:img3 tag:3];
    self.sView.tabBarItem=self.tbItem3;

    NSMutableArray *viewArr=[[NSMutableArray alloc] init];
    [viewArr addObject:self.navCon];
    [viewArr addObject:self.FnavCon];
    [viewArr addObject:self.SnavCon];


    self.tbCon=[[UITabBarController alloc] init];
    self.tbCon.viewControllers=viewArr;

    [self.window addSubview:tbCon.view];

    [self.window makeKeyAndVisible];

    return YES;
}