I am pushing one view controller to another view controller but when I am pushing the tabbar item index is not changing.How can I change selected index when pushing view from one to another.Thanks in advance.
Asked
Active
Viewed 9,896 times
1
-
tabbar item index ?.. you mean tab controller selected index right? – Shubhank Jan 24 '13 at 11:47
-
Please clarify the question some what more it's ambiguous. Need some more detail to give proper answer. – The iOSDev Jan 24 '13 at 11:55
-
read this documentation well https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITabBarController_Class/Reference/Reference.html – DD_ Jan 24 '13 at 12:01
-
how many viewcontrollers do you have? more than 6? – DD_ Jan 24 '13 at 12:04
-
when I am pushing view controller from 0 index of tabbarcontroller to another view controller which has 1 index . after pushing from 0 to 1 ,I want to show selectedIndex as 1. - Wolvorin – newDev Jan 24 '13 at 12:04
-
@newDev Did you even look at the documentation? tabBarController.selectedIndex will give you the index of the selected tab, and tabBarController.selectedViewController will give you a pointer to the view controller at the active tab. – DD_ Jan 24 '13 at 12:07
4 Answers
6
This line may help someone
[self.parentViewController.tabBarController setSelectedIndex:0];

Senthil
- 510
- 12
- 28
1
[self.tabBarController setSelectedIndex:0];
Here index 0 is refering to first tab of your tabbarcontroller.

Nimantha
- 6,405
- 6
- 28
- 69

Aman Aggarwal
- 3,754
- 1
- 19
- 26
0
simple example for UITabBarController
for You..only create three UIViewController
.this code work fine :)
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;
}

iPatel
- 46,010
- 16
- 115
- 137